Please Help. No build errors but program crashes and doesnt run again.

I cant figure out why this wont work. Everything work until the user enters their reply then it crashes. I am not getting any build errors and I am a little lost here. Please help.

Thanks in advance!

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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include <stack>
#include <iostream>
#include <string>
#include <sstream>

using namespace std;

int main() 
{
	stack <int> stack;
	string str;
	char token;
	char reply;
	bool Quit = false;

while(!Quit)
{
	cout << "Please enter the RPM expression you would like to have evaluated" << endl;

	getline(cin, str);

	int len = str.length();

	for (int i = 0; i < len; i++) 
	{
		token = str[i];

			if (token >= '0' && token <= '9') 
			{
				cout << "Token = " << token << " Push " << token << endl;
				int n = token - '0'; 
				stack.push(n);
			} 

			else if (token == '+') 
			{
				int a, b;
				a = stack.top();
				stack.pop();
				b = stack.top();
				stack.pop();
				cout << "Token = + Pop " << a << " Pop " << b << " Push " << a+b << endl;
				stack.push(a+b);
			} 

			else if (token == '-') 
			{
				int a, b;
				a = stack.top();
				stack.pop();
				b = stack.top();
				stack.pop();
				cout << "Token = - Pop " << a << " Pop " << b << " Push " << a-b << endl;
				stack.push(a-b);
			} 

			else if (token == '*') 
			{
				int a, b;
				a = stack.top();
				stack.pop();
				b = stack.top();
				stack.pop();
				cout << "Token = * Pop " << a << " Pop " << b << " Push " << a*b << endl;
				stack.push(a*b);
			} 
		
			else if (token == '/') 
			{
				int a, b;
				a = stack.top();
				stack.pop();
				b = stack.top();
				stack.pop();
				if (a == 0) {
				cout << "Error. Cannot divide by zero.\n";
			} 
			
			else 
			{
				cout << "Token = / Pop " << a << " Pop " << b << " Push " << b/a << endl;
				stack.push(b/a);
			}

			}
	}

		int answer = stack.top();
		stack.pop();

		if (!stack.empty()) 
		{
			cout << "The expression has an error.\n";
		} 
	
		else 
		{
			cout << "Answer: " << answer << endl;
		}

	cout << "" << endl;
	cout << "Would you like to quit Y / N" << endl;
        cin >> reply;

    reply = toupper(reply);
    if (reply == 'Y') 
		{
			Quit = true;
		}
}

	return 0;
}
After you cin>>reply there is a '\n' left in the stream.
If reply is 'Y" no problem since the program exits.
But if reply is 'N' you go to getline(cin, str);.
This returns an empty string since getline extracts all characters before a '\n' and then discards the '\n'. Remember there is still a '\n' in the stream.

To fix you must make sure the cin stream is empty.

An easy fix without using numeric limits is to getline into a string after the cin>>reply which will remove the '\n'.

1
2
3
4
5
6
7
8
9
10
11
12
...
	cout << "Would you like to quit Y / N" << endl;
        cin >> reply;
       getline(cin,str); /// now stream is empty, str's value will be changed at top of loop
    reply = toupper(reply);
    if (reply == 'Y') 
		{
			Quit = true;
		}
  }
	return 0;
}
Last edited on
Thanks vin,

Ill give this a try.
Topic archived. No new replies allowed.