Code not 'detecting' correct symbols.

Heya everyone,

I'm having a problem with some code that I basically used in quite the same fashion before in a different kind of program, but this time it's not working the way I want it to. Below is the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
char fileContent[MAX_NUMBER_OF_PLATFORMPIECES * 21] = {0};	//Variable to hold the file's contents.
string input;

//Some code stuff//

while(((int)fileContent[c] != 44) || ((int)fileContent[c] != 59)) {	//Append each next digit to the 'input' variable until a comma or semicolon is encountered.
	stringstream ss;	//Create a stringstream thing.
	string sToAppend;	//Create a string to use to append later on.
	ss << fileContent[c];	//Put fileContent[c]'s value into the stringstream thing.
	ss >> sToAppend;	//Put the stringstream thing's value into sToAppend.
	input.append(sToAppend);//Append sToAppend to the 'input' variable.
				//'input' now holds the old value + the new value.
	c++;
}

//Some more code stuff// 


My problem with this code is that it doesn't properly exit the while loop once it encounters a comma (decimal 44 in ascii) or a semicolon (decimal 59 in ascii). Image that fileContent = '500,500,0,0,0;', here after the second zero the while loop should exit because it encounters a comma, right? It doesn't.

Just something extra to note in case someone would otherwise suggest this: I've tried removing the (int) before each fileContent[c] but that has no effect whatsoever as far as I can tell, no positive nor negative effect.


Like I said at the beginning: I've used similar code before and there it did recognize the correct symbols.

Does anyone have any idea? If someone wants to entire code, I can post it if needed, but it uses the Allegro Game Library and some custom datafiles and headers which you don't have obviously, so testing it would be a bit harder to do.

Any and all replies are appreciated.
Last edited on
closed account (1yR4jE8b)
Why do you cast to integers? It is perfectly valid to compare chars directly, just make sure you are using single quotes and not double-quotes (those indicate strings, although they MAY work....but just to be sure use single quotes). I'm also pretty sure that your logical OR in your loop should be a logical AND. Think about it for a second, if your character is a semi-colon, it's not a comma so your loop will continue.

while(fileContent[c] != ',' && fileContent[c] != ':')
{
//stuff
}

also, you should really get aquainted with C++ style casts...C-Style casts are quite frowned upon when used in C++ code.

http://www.cplusplus.com/doc/tutorial/typecasting/
Last edited on
while(((int)fileContent[c] != 44) || ((int)fileContent[c] != 59))

This code will always be true because you have an OR condition.

You're running the loop while the char is not 44 OR not 59.

Say for example the char == 44. Then the != 59 check will be true, and therefore the OR will be true, and therefore the loop will continue.

You probably wanted AND instead of OR.

Also, you don't need to cast to int and use ASCII codes. You can just use the characters you want to compare with. I mean like.... try this instead:

while( (fileContent[c] != ',') && (fileContent[c] != ';') )

Then you don't need comments explaining what 44 and 59 are.


EDIT: doh, too slow. But still a helpful post!
Last edited on
I tried your method in that previous program I mentioned and then it didn't work, but I've probably done something wrong then. So I've changed my code so that it compares the chars directly (with single quotes) and still I'm having the same issue. The code still doesn't exit the loop once a comma or semicolon is found. =S

EDIT: Ofcourse Disch! I always confuse the way OR works -_-. It works now as far as the problem I posted about goes. Many thanks to the both of you who posted. =)
Last edited on
closed account (1yR4jE8b)
Haha, Disch you posted while I was editing mine after I realized that he need an AND.
Topic archived. No new replies allowed.