very hard question using stacks!!

hey everyone!! im facing a really hard question in my data structure course.
here is the question:

Given a number of digits your program is required to print the addition result of these numbers.
Write a C++ program that simply add numbers (using stacks), the program reads in floating point numbers, push them onto a stack, add them together whenever a plus sign, "+", is entered, and print the result. In addition, the adding machine will recognize the following instructions:
 A comma "," means a "cancel the last entry".
 A period "." means "cancel all entries", that is clear the stack.
 Making sure that the input is either a floating number or “+” or “,” or “.” characters.

I need someone to help me with this please :/
Here is the code I wrote but it doesn't seem to be right

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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <iostream>
using namespace std;

class CustomStack
{
	private:

		float* myStack;
		int size;
		int top;

	public:

		CustomStack(int sizeValue)
		{
			size = sizeValue;
			top = -1;
			myStack = new float[size];
		}

		float isEmpty()
		{
			if(top == -1)
			{
				return true;
			}
			return false;
		}

		bool isFull()
		{
			if(top == size-1)
			{
				return true;
			}
			return false;
		}

		void push(float element)
		{
			if(!isFull())
			{
				myStack[++top] = element;
			}
		}

		float pop()
		{
			return myStack[top--];
		}

		float peek()
		{
			return myStack[top];
		}
};

void main () 
{

float x;
float sum=0.0;
CustomStack stack (5);
cout<<"please enter a group of numbers"<<endl;
for (int i=0;i<5;i++)
{
	cin>>x;
	stack.push(x);
	sum=sum+x;
}


cout<<"please enter + , or ."<<endl; 
char sign;
cin>>sign;

		if (sign=='+')
		{
			cout<<sum<<endl;
		}
		else if (sign!='+')
		{
			if (sign==',')
			{
				stack.pop();
					cout<<"last input is deleted"<<endl;
					cout<<"new sum ="<<sum-stack.pop()<<endl;
			}
			while ((sign=='.') && (!stack.isEmpty()))
			{
				stack.pop();
			}
			cout<<"stack is empty"<<endl;
		}
	
  system ("pause");
}


I have 2 problems
first: I want the program to continue working even if the user inserted ',' or '.'
second: when the user inserts ',' it deletes the first input while I want it to delete the last input

I wish someone can help me with that
thank you in advance :)
After your if (sign ==',')
remove the line that says stack.pop();
That will fix your ',' problem. During the cout statement 2 lines below, the function will be called there.

In regards to making the program "continue", I'm not sure what you mean? You could simply place some of the main in a do while loop to continue running.
Last edited on
Topic archived. No new replies allowed.