problem with strings (maybe)

This topic is 3742 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[i]>>States::Anim[i]>>States::ctrl[i]>>
States::Velx[i]>>States::Vely[i]>>States::Accelx[i]>>
States::Accely[i]>>States::Type[i]>>States::NextState[i];
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[i]>>States::Anim[i]>>States::ctrl[i]>>		                 States::Velx[i]>>States::Vely[i]>>States::Accelx[i]>>
States::Accely[i]>>type>>States::NextState[i];
if (type == "S")
States::Type[i] = 0;
else if (type == "C")
States::Type[i] = 1;
else if (type == "A")
States::Type[i] = 2;
i++;
}
File.close();
};


States::Type[i] 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!

Share on other sites

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

Create an account

Register a new account

• Forum Statistics

• Total Topics
628704
• Total Posts
2984306

• 23
• 10
• 9
• 13
• 13