I have an input file that i make into a string, it looks like this

"1+2+3+45*5*2"

My problem is when i print the string it always prints as single characters
instead of 45 i get 4 5, how do i fix this. heres a small piece of the code.

cout<<string; //string content 1+2+3+45*5*2
int size=string.length();
cout<<string[7]<<endl;
cout<<string[8]<<endl;

output is always 4
5

You should pick a better name for your string other than "string", just to be safe. Aside from that, you're indexing your string so each index corresponds to a specific character, in the case of string[7] it'll be 5 and string[8] will be *.

If you want 45, you can test if the next character is an operator. If it isn't, then create a temp and concatenate the previous and the newly tested character to get your number. It's a suggestion.

Edited 5 Years Ago by Chilton: n/a

I have an input file that i make into a string, it looks like this

"1+2+3+45*5*2"

My problem is when i print the string it always prints as single characters
instead of 45 i get 4 5, how do i fix this. heres a small piece of the code.

cout<<string; //string content 1+2+3+45*5*2
int size=string.length();
cout<<string[7]<<endl;
cout<<string[8]<<endl;

output is always 4
5

Of course it is. A character of the string is 4. The next character of the string is 5. There is no character '45' -- that's two characters.

You have to separate the 'values' from the 'operators' and deal with multiple digits yourself.

This article has been dead for over six months. Start a new discussion instead.