Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  

Poll

What programming topic would you want the next challenge to be about?  (It might be a good opportunity to focus on a subject you're not familiar with or to reinforce knowledge on one that you already know)

Control Flow
- 2 (2.2%)
Arrays, Strings, Pointers, and References
- 8 (9%)
Functions
- 4 (4.5%)
Basic object-oriented programming
- 30 (33.7%)
A bit more advanced OOP (Composition, Operator overloading, Inheritance, Virtual Functions)
- 18 (20.2%)
Templates
- 8 (9%)
Other (Explain)
- 4 (4.5%)
Working with files?  (Streams)
- 15 (16.9%)

Total Members Voted: 89


Pages: 1 ... 36 37 [38] 39 40 ... 78

Author Topic: Programming Challenges & Resources (#bay12prog) Initiative  (Read 96035 times)

lordnincompoop

  • Bay Watcher
  • Allusionist
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #555 on: December 12, 2010, 06:31:52 pm »

Hm. A profiler goes with an IDE? I can't find any for my Geany.

Also, well, I might have found a critical bottleneck (or something) in my very idea. Can somebody tell me if any shenanigans are involved in compiling code written on Linux, on Windows? Especially if it involves SDL.

Also, is there any advantage in using short over integer, even if hypothetical?

Yes. It saves on memory space.
Logged

Virex

  • Bay Watcher
  • Subjects interest attracted. Annalyses pending...
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #556 on: December 12, 2010, 06:48:23 pm »

Some other operations, like division may also require less cycles if you're using a short.
Logged

Supermikhail

  • Bay Watcher
  • The Dwarf Of Steel
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #557 on: December 13, 2010, 08:46:39 am »

Yeah, finally bothered with a sizeof program. short is 2 bytes, int is 4 and long is 8 on my computer.

In other news, segfault has finally caught up with me, what do you know! Now I'm looking for a way to store a string of characters so that I could write it together with the rest of an object to a binary file. If I make it a char array with fixed length, it just doesn't compile, because sometimes it's apparently still just a pointer whatever I do.

For anybody curious or incredulous:
Code: [Select]
#include <iostream>
using namespace std;

class Element
{
public:
Element() {}
~Element() {}

Element(char[], short, double , short[] , short );

void DisplayName() const;
short GetNumber() const { return itsNumber; }
double GetElectronegativity() const { return itsElectronegativity; }
short GetValnumber() const { return itsValnumber; }
void DisplayValencies() const;

void AssignValues(char[], short, double , short[] , short );

private:
char itsName[20];
short itsNumber;
double itsElectronegativity;
short itsValency[7];
short itsValnumber;
};

Element::Element(char name[20], short number, double electronegativity, short valency[7], short valnumber)
{
itsName = name;
itsNumber = number;
itsElectronegativity = electronegativity;
itsValnumber = valnumber;
for(int i=0; i<valnumber; i++)
{
itsValency[i] = valency[i];
}
cout << "*Element constructor called*\n";
}

void Element::DisplayName() const
{
cout << itsName;
}

void Element::AssignValues(char name[20], short number, double electronegativity, short valency[7], short valnumber)
{
itsName = name;
itsNumber = number;
itsElectronegativity = electronegativity;
itsValnumber = valnumber;
for(int i=0; i<valnumber; i++)
{
itsValency[i] = valency[i];
}
cout << "*Values assigned*\n";
}

void Element::DisplayValencies() const
{
for(int i=0; i<itsValnumber; i++)
{
cout << itsValency[i];
if( i==(itsValnumber-1) )
cout << "." << endl;
else
cout << ", ";
}
}

Assign values is a copy of Element constructor, because I've decided to separate the program into reading and writing, and don't want to create two different objects, yet.
Logged

Shades

  • Bay Watcher
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #558 on: December 13, 2010, 08:57:15 am »

Is there a reason your using char arrays for strings rather than stl::string? (which also has nice streaming operations if you want conversion to a byte buffer)
Logged
Its like playing god with sentient legos. - They Got Leader
[Dwarf Fortress] plays like a dizzyingly complex hybrid of Dungeon Keeper and The Sims, if all your little people were manic-depressive alcoholics. - tv tropes
You don't use science to show that you're right, you use science to become right. - xkcd

Supermikhail

  • Bay Watcher
  • The Dwarf Of Steel
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #559 on: December 13, 2010, 09:19:45 am »

Well, er, because I don't know how to use it properly, and my book on C++ mentions it only teasingly... Oh, it's stl::! Does that mean I can using namespace it? So using stl::string really won't be considered an overkill?
Logged

Shades

  • Bay Watcher
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #560 on: December 13, 2010, 09:39:44 am »

You can using namespace it, just make sure if you do that you put the using in source not header files. It works in fine in both of course but in the headers leads to trouble.

Where I work there is a set of 5 using namespaces in a generic header which causes no end of problems when we get name clashes... which is often :( poor poor codebase (/rant)
Logged
Its like playing god with sentient legos. - They Got Leader
[Dwarf Fortress] plays like a dizzyingly complex hybrid of Dungeon Keeper and The Sims, if all your little people were manic-depressive alcoholics. - tv tropes
You don't use science to show that you're right, you use science to become right. - xkcd

Supermikhail

  • Bay Watcher
  • The Dwarf Of Steel
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #561 on: December 13, 2010, 10:05:25 am »

Yep. Turns out I don't know how to use string. How do I use it? Can't I have string as a return value? I've looked at a C++ reference, and got no idea.
Logged

Shades

  • Bay Watcher
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #562 on: December 13, 2010, 10:12:51 am »

Treat it like any other object you have, you can return it or references or pointers to it happily and you can add it to other strings or to char* and char[] to concatenate the string into a new one. It has a length() method and overloads the comparison operators if you need to compare it. I think it also has a equals() method as well which is more strcmp like.

Finally it has a c_str() method if you really really need to convert it to a char*
Logged
Its like playing god with sentient legos. - They Got Leader
[Dwarf Fortress] plays like a dizzyingly complex hybrid of Dungeon Keeper and The Sims, if all your little people were manic-depressive alcoholics. - tv tropes
You don't use science to show that you're right, you use science to become right. - xkcd

Supermikhail

  • Bay Watcher
  • The Dwarf Of Steel
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #563 on: December 13, 2010, 11:37:38 am »

Uh... how do I declare a constructor with it? As far as I can manage, it doesn't like either Element(std::string ), or Element(std::string &).
Logged

Shades

  • Bay Watcher
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #564 on: December 13, 2010, 11:46:56 am »

Both should work fine, although I tend to use Element(const std::string&)

Edit: Make sure your including <string> as well, you probably are but it can't hurt to mention it.
« Last Edit: December 13, 2010, 11:49:12 am by Shades »
Logged
Its like playing god with sentient legos. - They Got Leader
[Dwarf Fortress] plays like a dizzyingly complex hybrid of Dungeon Keeper and The Sims, if all your little people were manic-depressive alcoholics. - tv tropes
You don't use science to show that you're right, you use science to become right. - xkcd

ILikePie

  • Bay Watcher
  • Call me Ron
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #565 on: December 13, 2010, 12:22:16 pm »

It seems C and Java like writing to files in very different ways. I have two programs do the same thing:
Spoiler (click to show/hide)
and yet, their output is very different
Spoiler (click to show/hide)
Notice how C writes it's bytes in one way, (00aa) and Java writes them backwards (aa00). How do I write bytes to a file in Java in the same order they are written in C?
Logged

qwertyuiopas

  • Bay Watcher
  • Photoshop is for elves who cannot use MSPaint.
    • View Profile
    • uristqwerty.ca, my current (barren) site.
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #566 on: December 13, 2010, 12:52:30 pm »

C's byte order is the same as your system's. Java is system-independant, and orders the bytes the same way on all computers.

See http://en.wikipedia.org/wiki/Endianness for details about byte order, or look up something specific to Java or C.
Logged
Eh?
Eh!

Supermikhail

  • Bay Watcher
  • The Dwarf Of Steel
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #567 on: December 13, 2010, 02:01:05 pm »

Well, that's fancy. Segfault, I mean.

For anyone curious:
Code: [Select]
//element.hpp

#include <iostream>
#include <string>

class Element
{
public:
Element() {}
~Element() {}

Element( const std::string &, short &, double &, short [], short &);

std::string GetName() const { return itsName; }
short GetNumber() const { return itsNumber; }
double GetElectronegativity() const { return itsElectronegativity; }
short GetValnumber() const { return itsValnumber; }
void DisplayValencies() const;

void AssignValues( const std::string , short, double , short[] , short );

private:
std::string itsName;
short itsNumber;
double itsElectronegativity;
short itsValency[7];
short itsValnumber;
};

Element::Element( const std::string & name, short &number, double &electronegativity, short valency [], short &valnumber)
{
itsName = name;
itsNumber = number;
itsElectronegativity = electronegativity;
itsValnumber = valnumber;
for(int i=0; i<valnumber; i++)
{
itsValency[i] = valency[i];
}
std::cout << "*Element constructor called*\n";
}

void Element::AssignValues(const std::string name, short number, double electronegativity, short valency[], short valnumber)
{
itsName = name;
itsNumber = number;
itsElectronegativity = electronegativity;
itsValnumber = valnumber;
for(int i=0; i<valnumber; i++)
{
itsValency[i] = valency[i];
}
}

void Element::DisplayValencies() const
{
for(int i=0; i<itsValnumber; i++)
{
std::cout << itsValency[i];
if( i==(itsValnumber-1) )
std::cout << ".\n";
else
std::cout << ", ";
}
}

Code: [Select]
//element_setup.cpp

#include <iostream>
#include <fstream>
#include "element.hpp"
#include <string>

using namespace std;

int menu()
{
int choice;
cout << "****Menu****\n\n";
cout << "(1)Write.\n";
cout << "(2)Read.\n";
cout << "(3)Quit.\n\n";
cout << ": ";
cin >> choice;
return choice;
}



int main(int argc, char** argv)
{
string name;
short number;
double electronegativity;
short valency[7];
short valnumber;

cout << name.length();

int choice;
while((choice=menu())!=3)
{
if (choice == 1)
{
cout << "Enter the desired element's name: ";
cin >> name;
cout << "\nEnter " << name << "'s number in the Periodical Table: ";
cin >> number;
cout << "\nEnter " << name << "'s electronegativity: ";
cin >> electronegativity;
cout << "\nEnter " << name << "'s overall number of valencies: ";
cin >> valnumber;
for (int i = 0; i<valnumber; i++)
{
cout << "Enter valency number " << i+1 << ": ";
cin >> valency[i];
}
cout << "\n";

Element elemwrite(name, number, electronegativity, valency, valnumber);

ofstream fout("elements_data", ios::binary);
if( !fout )
{
cout << "Unable to open elements data for writing" << endl;
return 1;
}

fout.write((char*) & elemwrite, sizeof elemwrite);

fout.close();
}
else
if (choice == 2)
{
ifstream fin("elements_data", ios::binary);
if( !fin )
{
cout << "Unable to open elements data" << endl;
return 1;
}
else

Element elemread(name, number, electronegativity, valency, valnumber);

fin.read((char*) & elemread, sizeof elemread);

cout << "Element read\n";

cout << "Element " << elemread.GetName();
cout << "'s number in the Periodical Table is ";
cout << elemread.GetNumber() << ".\n";
cout << "Its electronegativity is ";
cout << elemread.GetElectronegativity() << ".\n";
valnumber = elemread.GetValnumber();
if( valnumber == 1 )
cout << "It has single possible valency which is ";
else
{
cout << "Its overall number of possible valencies is ";
cout << valnumber << " which are ";
}
elemread.DisplayValencies();
cout << "\n";

fin.close();
}
else
cout << "Please select again!\n";
}

return 0;
}

Well, it seems I can't make it output a string of characters to a binary file correctly. Which means I have to resort to a text file. :( And then write an encryption function for it.
Logged

qwertyuiopas

  • Bay Watcher
  • Photoshop is for elves who cannot use MSPaint.
    • View Profile
    • uristqwerty.ca, my current (barren) site.
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #568 on: December 13, 2010, 02:23:23 pm »

You might be writing a few pointers, and when they are read back, the memory they point to might not exist anymore. Probably the string.

Perhaps write a method that, when passed an ofstream, will write the data to it, and one for reading from an ifstream? The string would need to be converted to a plain char array, either manually or by some string method.
Logged
Eh?
Eh!

Supermikhail

  • Bay Watcher
  • The Dwarf Of Steel
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #569 on: December 13, 2010, 02:47:45 pm »

That will probably involve writing character by character, and I can only dream of conceiving something that will read such a file correctly. And I suspect then there's no need to use string.

Well, tonight the world either changes, or is destroyed in a fiery catsplosion when I'm so depressed after a failed attempt to reconcile my pointers that I'll spend the rest of the night playing DF.
Logged
Pages: 1 ... 36 37 [38] 39 40 ... 78