Problem converting string/char to int using atoi

I'm creating a program that solves postfix expressions. I used an algorithm that uses stacks and implements it through vectors.

A sample input might look like this:


Enter the expression: 9 2 1 + / 4 *


Now every operand/constant must be pushed onto the stack and when an operator is encountered in the input, it will be applied to the two topmost operands in the stack. The two topmost operands in the stack will then be popped and the result of the operation will be pushed back to the stack.

Ideally, I must use a character array to store values of the tokens (operands and operators). I will just have to convert numbers into ints. So I used this in the input:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
cout << "Enter the expression: ";
	while ( i < 30 ) {
		cin.get( expression[ i ] );
		if ( expression[ i ] == '\n' )
			break;	
		i++;
	}
		
	int SIZE = i / 2 + 1;
	
	for ( i = 0; i < SIZE; i++ )
		token[ i ] = expression[ i * 2 ];

	token[ SIZE ] = ' ';
	SIZE++;


Turns out, the spaces in between were still read and so I used the latter for-loop to remove them and store in an array only the tokens. It worked well for me.

But then, I encountered problems in the output. My program was converting and pushing into the stack wrong values. For example using the sample input above,
it pushes to the stack and outputs a value of 99 instead of 9 for the 1st operand. For the 2nd operand it prints and pushed 29 instead of 2 and for the third it prints and pushes 19 instead of 1.

My operation functions to evaluate contents of the stack is doing fine since it produces correct results (e.g. 19 + 29 = 48) but problem is these are wrong values. Here is the ideal output:


Token = 9       Push    9
Token = 2       Push    2
Token = 1       Push    1
Token = +       Pop     1       Pop     2       Push    3
Token = /       Pop     3       Pop     9       Push    3
Token = 4       Push    4
Token = *       Pop     4       Pop     3       Push    12
Token =         Pop    12


And my output looks like this:


Token = 9       Push   99
Token = 2       Push   29
Token = 1       Push   19
Token = +       Pop    19       Pop    29       Push   48
Token = /       Pop    48       Pop    99       Push    2
Token = 4       Push   49
Token = *       Pop    49       Pop     2       Push   98
Token =         Pop    98


If you notice, when operators are encountered it applies it correctly and produces correct results (basing from the contents of the stack) though the contents of the stack is wrong. Notice that operands also include an extra 9 (e.g. 99 instead of 9, 29 as opposed to 2, 49 - 4, 19 - 1)

I guess the problem is with converting of operands, or possibly the input of tokens.

Here's my code for converting char to int and pushing it into the stack:

1
2
3
4
5
else {
			constant = token[ i ];
			mySTACK.push_back( atoi( &constant ) );
			cout << "\tPush " << setw(4) << atoi( &constant ) << endl;
		}


BTW, I used cstdlib library to utilize the atoi function.

Any suggestions on how I can solve this problem? Any form of help appreciated :) Thanks a lot.
Can you show the declaration for mySTACK and constant?
atoi requires a C-String (i.e. a null character terminated array of char). If all you're giving it is the address of one char then it won't be null character terminated.

Since you're dealing only with digits, you could make a two character array whose first character is your constant char and whose second value is the null character, '\0' and send that to atoi.

e.g.

1
2
3
4
5
6
7
8
9
10
11
char constant[2];
constant[1] = '\0';

...

else
{
   constant[0] = token[ i ];
   mySTACK.push_back( atoi(constant) );
   cout << "\tPush " << setw(4) << atoi(constant) << endl;
}
Topic archived. No new replies allowed.