program exits

I have a program that compiles, but immediately after it gets to a certain point it exits unexpectedly.

Here is the program:

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
//typedef int ValueType;
//declarations blah blah

int main(void)
{
    provideHelpIfNecessary();
    while(true){
    cout << "Expression? ";
      string expr;                    // holds the supposed expression
      getline(cin, expr);
      if (expr.length() == 0)
	  break;
      try {
         ValueType result = processExpression(expr);
         cout << result << endl;
      } catch (ZeroDivideException ex) {
         cerr << "Attempt to divide by zero!\n";
      } catch (IllegalExprException ex) {
         cerr << "Illegal infix expression!\n";
      }
    }  // end while
   cout << endl;
} // end main

//provideHelpIfNecessary()

ValueType processExpression(const string& expr) 
   throw (IllegalExprException, ZeroDivideException)
{
    stack<ValueType> ValStack;
    stack<char> OpStack;
    for (int i = 0; i < expr.length(); i++)
	{
	    if (expr[i] == '0'|| expr[i] == '1'|| expr[i] == '2'|| expr[i] == '3'|| expr[i] == '4'|| expr[i] == '5'|| expr[i] == '6'|| expr[i] == '7'|| expr[i] == '8'|| expr[i] == '9') ValStack.push(expr[i]);
	    if (expr[i] == '(') OpStack.push(expr[i]);

	    if (expr[i] == '*'|| expr[i] == '+'|| expr[i] == '-'|| expr[i] == '/') {
		if(OpStack.empty()) OpStack.push(expr[i]);
		else if (precedence(expr[i]) > precedence(OpStack.top())) OpStack.push(expr[i]);
		else 
		    {
			if (!OpStack.empty() && precedence(expr[i]) <= precedence(OpStack.top())) execute(ValStack,OpStack);
			OpStack.push(expr[i]);
		    }}
	    if (expr[i] == ')') {
		if (OpStack.top() != '(') execute(ValStack,OpStack);
		OpStack.pop();
	    }}

    if (!OpStack.empty()) execute(ValStack,OpStack);
    ValueType result = ValStack.top();
    return 0;
} // end processExpression

//isValidResponse(response)
//isYesResponse(response)

int precedence(char op)
{
    if (op == '*' || op == '/') op = 3;
    if (op == '+' || op == '-') op = 2;
    if (op == '(') op = 0;
    else op = -1;
    return op;
} // end precedence

void execute(stack<ValueType>& valStack, stack<char>& opStack)
   throw (IllegalExprException, ZeroDivideException)
{
    ValueType result;
    ValueType operand2 = valStack.top(); 
    valStack.pop();
    ValueType operand1 = valStack.top(); 
    valStack.pop();
    char op = opStack.top();
    opStack.pop();
    result = doOperation(operand2,op,operand1);
    valStack.push(result);

} // end execute

ValueType doOperation(ValueType operandL, char operation, ValueType operandR)
   throw (IllegalExprException, ZeroDivideException)
{ 
    ValueType ans;
    if (operation == '*') ans = operandL * operandR;
    if (operation == '/') ans = operandL / operandR; 
    if (operation == '+') ans = operandL + operandR;
    if (operation == '-') ans = operandL - operandR; 
    return ans;
} // end doOperation 
Last edited on
and at what point is it exiting?
sorry.

this is the output:
1
2
3
4
5
6
Do you need help (Y/N)?
y
Enter an infix expression at the prompt.
The program will tell you its value.
To stop the program, just hit 'return' at the prompt.
Expression? 
ah i bet i know what the issue is. i just had this at a programming competition
put this on line... well wait i cant tell you what line because you didnt supply providehelpifneccesary.
anyways put this right after expression?: cin.ignore(std::numeric_limits<std::streamsize>::max())
But by the way, I'm not allowed to change anything in main(void), it's pre-written by my professor. Is there any other solution?
post the providehelpifneccesary function
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
void provideHelpIfNecessary(void)
{
    char answer;
    cout <<"Do you need help (Y/N)?\n";
    cin >> answer;
    if (isValidResponse(answer) == false) {
	cout << "Invalid response! Try again.\n";
	provideHelpIfNecessary();
    }
    else if (isValidResponse(answer) == true) {
        if (isYesResponse(answer) == true) {
            cout << "Enter an infix expression at the prompt.\nThe program will tell you its value.\nTo stop the program, just hit 'return' at the prompt.\n";
        }
	else return; }

} // end provideHelpIfNecessary

bool isValidResponse(char response)
{
    bool valid = false;
    if (response == 'Y'|| response == 'y'|| response == 'T'|| response == 't'||
        response == '1'|| response == 'N'|| response == 'n'|| response == 'F'||
        response == 'f'|| response == '0') return valid = true;
    else return valid;
} // end isValidResponse

bool isYesResponse(char response)
{
    if ( response == 'Y'|| response == 'y'|| response == 'T'|| 
         response == 't'|| response == '1') return true;
    else return false;
} // end isYesResponse 
post it right below cin >> answer
isn't it going to only apply for provideHelpIfNecessary() that way?
no because cin is a global object and so is its buffer
Now this error message is coming up:
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
proj3.cc: In function ‘void provideHelpIfNecessary()’:
proj3.cc:98:16: error: ‘numeric_limits’ is not a member of ‘std’
     cin.ignore(std::numeric_limits<std::stream_size>::max());
                ^
proj3.cc:98:36: error: ‘stream_size’ is not a member of ‘std’
     cin.ignore(std::numeric_limits<std::stream_size>::max());
                                    ^
proj3.cc:98:59: error: no matching function for call to ‘max()’
     cin.ignore(std::numeric_limits<std::stream_size>::max());
                                                           ^
proj3.cc:98:59: note: candidates are:
In file included from /usr/include/c++/4.8.2/bits/char_traits.h:39:0,
                 from /usr/include/c++/4.8.2/ios:40,
                 from /usr/include/c++/4.8.2/ostream:38,
                 from /usr/include/c++/4.8.2/iostream:39,
                 from proj3.cc:17:
/usr/include/c++/4.8.2/bits/stl_algobase.h:216:5: note: template<class _Tp> const _Tp& std::max(const _Tp&, const _Tp&)
     max(const _Tp& __a, const _Tp& __b)
     ^
/usr/include/c++/4.8.2/bits/stl_algobase.h:216:5: note:   template argument deduction/substitution failed:
proj3.cc:98:59: note:   candidate expects 2 arguments, 0 provided
     cin.ignore(std::numeric_limits<std::stream_size>::max());
                                                           ^
In file included from /usr/include/c++/4.8.2/bits/char_traits.h:39:0,
                 from /usr/include/c++/4.8.2/ios:40,
                 from /usr/include/c++/4.8.2/ostream:38,
                 from /usr/include/c++/4.8.2/iostream:39,
                 from proj3.cc:17:
/usr/include/c++/4.8.2/bits/stl_algobase.h:260:5: note: template<class _Tp, class _Compare> const _Tp& std::max(const _Tp&, const _Tp&, _Compare)
     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
     ^
/usr/include/c++/4.8.2/bits/stl_algobase.h:260:5: note:   template argument deduction/substitution failed:
proj3.cc:98:59: note:   candidate expects 3 arguments, 0 provided
     cin.ignore(std::numeric_limits<std::stream_size>::max());
                                                           ^
#include <limits>
I'm still getting the errors from line 1 - 7 from my previous post, after I #include <limiits>
1) it is limits, not limiits
2) it is streamsize, not stream_size
Topic archived. No new replies allowed.