Problem storing characters to array using a single line of input

So I want to get a postfix expression, in just a single line, and store every single character in that expression to a character array. For example, I want to enter this expression:

9 2 1 + / 4 *


BTW, I am only using single-digit integers. So, I don't need a string array.

Problem is, when I finish the expression and press ENTER, I get no results. Plus, the program doesn't end properly so I have to press CTRL + C. I tried two schemes but none worked.

Here are the code snippets of the schemes I tried to use for the input:

1
2
3
4
5
6
7
int i = 0;
cout << "Enter the expression: ";

while ( token[ i - 1 ] != '\n' ) {
	cin >> token[ i ];
	i++;
}



1
2
3
4
5
6
7
int i = 0;
while ( i < 30 ) {
	cin >> token[ i ];
	if ( token[ i ] == '\n' )
		break;	
	i++;
}


Any suggestions, please, on how to solve this?? Thanks a lot :)
Try
1
2
3
4
while ( token[ i - 1 ] != '\n' ) {
     cin.get(token[ i ]);
     i++;
}

http://www.cplusplus.com/reference/iostream/istream/get
Just noticed that you are checking the value of token[i-1] and when i is 0 you are indexing out side of the array bounds. Make sure you fix that if you keep that version.
Last edited on
Topic archived. No new replies allowed.