Public Group

problem with strings (maybe)

This topic is 4024 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

To initialize a class i use a txt file. something like this:
States::States()
{
ifstream File;
File.open ("States.txt");
int i = 0;
while (!File.eof())
{
File>>States::State>>States::Anim>>States::ctrl>>
States::Velx>>States::Vely>>States::Accelx>>
States::Accely>>States::Type>>States::NextState;
i++;
}
File.close();
};


a line in States.txt would look like this: 45 45 0 0 -5 0 0.25 2 46 the data members are either int or float, and this way worked perfectly. Then since i will need to treat text in another class anyway, i thought i would change one of the values in the txt file to actual text, not only to exercise but also to make the txt file more readable to me. the above line now would look like this: 45 45 0 0 -5 0 0.25 A 46 The penultimate value would carry either a S, a C or an A. (they stand for stand, crouch, air). States constructor would change like this:
...

#include <string>

...

States::States()
{
ifstream File;
File.open ("States.txt");
string type;
int i = 0;
while (!File.eof())
{
File>>States::State>>States::Anim>>States::ctrl>>		                 States::Velx>>States::Vely>>States::Accelx>>
States::Accely>>type>>States::NextState;
if (type == "S")
States::Type = 0;
else if (type == "C")
States::Type = 1;
else if (type == "A")
States::Type = 2;
i++;
}
File.close();
};


States::Type determines what kind of hitbox a certain animation gets amongst other things. Now that kind of worked, but with something funny: the very first element(with index 0) would get the hitbox type of the last element in the file(so changing the last element type will give another result). Even better, this happens only when i run the program without debugging. When i run in the debug mode everything is fine, just like with the 1st method. That's about it. since i never really used strings i'm tempted to believe the error is because of them, although i don't really know at this point. Not to mention that it works in debug, making it useless. Can someone help?

Share on other sites
.eof() only becomes true after you try to read after the EOF. That is, you have to detect the EOF after attempting to read, but before applying the results of your reading. In short:

while (File)
{

if (!File) break;

// Copy temporary to array
}

Share on other sites
Many thanks! That solved it!

1. 1
2. 2
3. 3
Rutin
19
4. 4
5. 5

• 9
• 19
• 9
• 31
• 16
• Forum Statistics

• Total Topics
632617
• Total Posts
3007459

×

Important Information

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!