Need help with a C++ stack program

Ok so basically what i have to do is prompt the user for 3 numbers and push them onto a stack (this is to be done in the input() function)

i also need an evaluate function that calls the input() function and that pops the first two numbers on the stack, perform an operation inputted by the user (+, -, *...)etc and pushes that result back to the top of the stack...this happens one more time until only one item remains in the stack

so for example i enter the postfix expression " 4 3 5 * + " it should put these 3 numbers on a stack...pop 5..pop 3 then multiply them to get 15..push 15 back on top...pop 15..pop 4..add them together and push 19 back on the stack..so that 19 is the only thing on the stack

ok heres what i have so far

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
#include <iostream>
#include <stack> //stack header file

using namespace std; 

stack <int> operands;

int main() 
{
	
}



void input() //user inputs 3 items into a stack
{
	cout << "Input three numbers into a stack (Enter a number then press Enter)" << endl;  
	cin >> item1; 
	operands.push(item1);
	cin >> item2;
	operands.push(item2);
	cin >> item3; 
	operands.push(item3); 
	
}

void evaluate() //evaluate function operations included
{
	item3 = operands.top(); 
	operands.pop();
	item2 = operands.top(); 
	operands.pop(); 
	item2 = item3*item2; 
	operands.push(item2); 
	operands.pop();
	operands.pop();
	item1 = item2+item1; 
	operands.push(item1); 
	cout << item1 << endl; 
	
}



my problems are
what do i put in the main?, how can i read in operands (+, -, *, /...)instead of physcally doing it in the program myself (see where i did item3*item2, and item1=item2+1;)
and how do i call input() in the evaluate function i cant figure out how to pass item1 item2 and item3 to it because they arent defined until the user inputs a value

Last edited on
1. Your main function should be what do you want to run, and how many times do you want to run it. Make sure you have the functionality in the input() though. Make sure to clear the stack after you are done using it.

1
2
3
4
5
6
7
8
9
10
11
12
13

int main(){

char a;
while(a == 'y')
{
     cout<<"Do you want to enter input?";
     input();
}

}



2. Create global variables and declare them, then you can use them anywhere in the program.

3. If I were you, I would put everything into a class. Optional.
well this is what i just finished now and it works perfectly except im not following proper directions in which i should have a separate input and evaluate function..i just put everything into the main..
it takes 3 items into a stack and does the operations inputted also

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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <iostream>
#include <stack> //stack header file

using namespace std; 

stack <int> operands;


int main() 
{
	int item1, item2, item3; //push 3 input items onto a stack and ask for the two operators
	char op, op2; 
	cout << "Enter your postfix expression" << endl;
	cout << "(Enter three numbers followed by two operators, one per line)" << endl;  
	cin >> item1; 
	operands.push(item1);
	cin >> item2;
	operands.push(item2);
	cin >> item3; 
	operands.push(item3);
	cin >> op;
	cin >> op2; 

	if(op =='+')  //ask for the first operator and do the destignated operation
		item2 = item3+item2; 
	if(op=='*')
		item2 = item3*item2; 
	if(op=='-')
	item2 = item3-item2;
	if(op=='/')
	item2 = item3/item2; 
	if(op=='%')
		item2 = item3%item2;

	item3 = operands.top();  //ask for the second operator and do the destignated operation
	operands.pop();
	item2 = operands.top(); 
	operands.pop(); 
	if(op =='+') 
		item2 = item3+item2; 
	else if(op=='*')
		item2 = item3*item2; 
	else if(op=='-')
		item2 = item3-item2;
	else if(op=='/')
		item2 = item3/item2; 
	else if(op=='%')
		item2 = item3%item2;
	else 
		cout << "You have entered an incorrect operator" << endl; 
	operands.push(item2); //push the result back to top of stack

	operands.pop();
	operands.pop();

	if(op2 =='+') 
		item1 = item2+item1;
	else if(op2=='*')
		item1 = item2+item1;
	else if(op2=='-')
		item1 = item2-item1;
	else if(op2=='/')
		item1 = item2/item1;
	else if(op2=='%')
		item1=item2%item1; 
	else
		 cout << "You have entered an incorrect operator" << endl; 
	operands.push(item1); //push result back to top of stack
	cout << "The number remaining in the stack after those operations is " << item1 << endl; 

}

Cre
ate global variables and declare them, then you can use them anywhere in the program.

Don't do this. Global variables are a lazy shortcut that can lead to all sorts of problems once you start writing bigger programs. Don't get into bad habits. If part of the purpose of this exercise is to learn how to write and call functions with arguments, then embrace that, and start the process of learning how to write well-structured code.

EDIT: From looking at the code in the OP, it looks as though the first thing you ought to do is go back to your textbook and remind yourself how you pass arguments into functions.
Last edited on
unfortunately i still need help...nothing happens when i run this code..i have the right stuff in input() and evaluate() but i cant link it all together
i still dont get what to put in the main either

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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <iostream>
#include <stack> //stack header file

using namespace std; 

stack <int> operands;


int main() 
{
	int input(); 
	void evaluate(); 

}



int input(int, char) //user inputs 3 items into a stack
{
	int item1, item2, item3; //push 3 input items onto a stack and ask for the two operators
	char op, op2; 
	cout << "Enter your postfix expression" << endl;
	cout << "(Enter three numbers followed by two operators, one per line)" << endl;  
	cin >> item1; 
	operands.push(item1);
	cin >> item2;
	operands.push(item2);
	cin >> item3; 
	operands.push(item3);
	cin >> op;
	cin >> op2; 
	return op, op2, item1, item2, item3;
	
}

void evaluate(char &op, char &op2, int &item1, int &item2, int &item3) //evaluate function operations included
{
	

	item3 = operands.top();  //ask for the second operator and do the destignated operation
	operands.pop();
	item2 = operands.top(); 
	operands.pop(); 
	if(op =='+') 
		item2 = item3+item2; 
	else if(op=='*')
		item2 = item3*item2; 
	else if(op=='-')
		item2 = item3-item2;
	else if(op=='/')
		item2 = item3/item2; 
	else if(op=='%')
		item2 = item3%item2;
	else 
		cout << "You have entered an incorrect operator" << endl; 
	operands.push(item2); //push the result back to top of stack

	operands.pop();
	operands.pop();

	if(op2 =='+') 
		item1 = item2+item1;
	else if(op2=='*')
		item1 = item2+item1;
	else if(op2=='-')
		item1 = item2-item1;
	else if(op2=='/')
		item1 = item2/item1;
	else if(op2=='%')
		item1=item2%item1; 
	else
		 cout << "You have entered an incorrect operator" << endl; 
	operands.push(item1); //push result back to top of stack
	cout << "The number remaining in the stack after those operations is " << item1 << endl; 
	
}

Last edited on
The problem is that you seem to have misunderstood how to pass data into functions, and how to return data from functions to the calling code.

You say:

nothing happens when i run this code

but I have a hard time believing that you've ever run this code, because it shouldn't even compile. You've defined the input function to take two arguments, but you're attempting to call it with no arguments, which is illegal. Similarly, you've defined evaluate to take 5 arguments, but are trying to call it with no arguments.

You need to go back to your textbook and learn how functions work.

How are you possibly able to try and run it, when it doesn't even compile?

EDIT: Oops, I wasn't looking at the code closely enough. As cire points out below, it will compile.
Last edited on
How are you possibly able to try and run it, when it doesn't even compile?

It compiles just fine: http://ideone.com/r4L70S

The (primary) problem is that lines 11 and 12 are interpreted as function declarations so that the code may as well be:

1
2
3
int main()
{
}
Last edited on
i cant find an example of what i am trying to do ANYWHERE and im starting to get really frustrated (ive used google and my textbook)...all i find is simple functions that pass numbers that have already been defined..

hence why i resorted to posting here but i give up
all i find is simple functions that pass numbers that have already been defined..


You must pass variables that have already been defined. You can't, after all, pass variables that don't exist. Your input function doesn't even reference the parameters it "takes".

return op, op2, item1, item2, item3; is the same as return item3;
You may want to look up the comma operator to understand why.
Last edited on
i need to call input() within evaluate() so i can use the values the user entered (which are item1, item2, item3, op, and op2) and none of these are defined until the user enters a number/char for them i just cant pass them?? then what i am trying to do is impossible
i need to call input() within evaluate() so i can use the values the user entered (which are item1, item2, item3, op, and op2) and none of these are defined until the user enters a number/char for them i just cant pass them?? then what i am trying to do is impossible


Those variables are defined in your original code before the user enters a number/char for them. What makes you think they cannot be defined prior to the user entering them now?


Here's one possible (incomplete) implementation, just so you can see how it might work:
http://ideone.com/vARynI

Here's one possible (incomplete) implementation, just so you can see how it might work:
http://ideone.com/vARynI


thanks a lot this helped
It compiles just fine

Oops, sorry, yes, you're right. I wasn't looking carefully enough.
Topic archived. No new replies allowed.