Sign in to follow this  

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
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)
{
// Read into temporary

if (!File) break;

// Copy temporary to array
}

Share this post


Link to post
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.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this