Cstring Question

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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50

// 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
Last edited on
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

if you want to change a character to number use atoi() etc.
http://www.cplusplus.com/reference/clibrary/cstdlib/atoi/

check out <cstdlib>
Thanks matsom for the help.
Topic archived. No new replies allowed.