I have a question about converting a Cstring into the right out put. I am working through this code 1 problem at a time. There is multiple other things I am going to do with the ( char strIn ) but I want to get this part down before moving on.
// This is a function. There is a class Stack this is working with
int PostFixCalcu(char strIn[])
{
StackType* ptr = new StackType;
while( strIn[ i ] != '\0' )
{
switch( strIn[ i ] )
{
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
case 8:
case 9:
strIn[ i ] = strIn[ i ];
break;
case 42:
strIn[ i ] = '*'; // *
break;
case 43:
strIn[ i ] = '+'; // +
break;
case 45:
strIn[ i ] = '-'; // -
break;
case 47:
strIn[ i ] = '/'; // /
break;
}
if( strIn[ i ] != 43 )
{
ptr->Push( strIn[ i ] );
cout << strIn[ i ] << " is pushed." << endl;
}
else
{
ptr->Push( ( static_cast< char >( strIn[ i ] ) ) );
}
i++;
}
cout << ptr->Pop();
return 0;
}
when I run the code with input 24+ I get:
2 is pushed
4 is pushed
43
How do I get the 43 into a + sign and make it perform
like a + sign.
Thanks
char x = 0; =/= char x = '0'; your switch might not work correctly
variable "i" was never initialized -you could be going beyond your bounds
lines 20,23,26,29,32 makes no sense to me :s
I did not display on the screen but i is global and initialized to 0.
I have no idea what the input is going to be. It could be any # between 0-9 along with the 4 operators (+,-,*, /).
Line 20 is corrected.
In lines 23,26,29,32 I was trying to do was filter out the operators and assign the char to the number. Instead of 43 I would get a + sign.
you're still not comparing your characters correctly, either use case'0': or case 48: -both are equivalent, teh former is more readable to humans though