cannot get program to end if user enters quit

I cant get the program to terminate if the the user enters exit. Not sure what I did wrong. Thanks


int main()

{
string input;
int operand1, operand2;
char charOperator;

do

{

cout << "Please enter an expression without any spaces (enter exit to quit): ";

cin>> input;

if(input.compare("exit") == 0)

{

cout << "\nThank you for using my calculator! Welcome to come back!\n";

exit(0);

}

for(int i = 0; i < input.length(); i++)

{

if(input[i] =='+'|| input[i] =='-'||input[i] =='*'|| input[i] =='/'|| input[i]=='%')

{
operand1 = stoi(input.substr(0, i));

charOperator = input[i];

operand2 = stoi(input.substr(i + 1));
}

}

switch(charOperator)

{

case '+':

{

cout << "The result of the expression is: " << (operand1 + operand2) << endl;

break;

}

case '-':

{

cout << "The result of the expression is: " << (operand1 - operand2) << endl;

break;

}

case '*':

{

cout << "The result of the expression is: " << (operand1 * operand2) << endl;

break;

}

case '/':

{

if(operand2 == 0)

cout << "ERROR: Division by zero!\n";

else

cout << "The result of the expression is: " << (operand1 / operand2) << endl;

break;

}

case '%':

{

if(operand2 == 0)

cout << "ERROR: Division by zero!\n";

else

cout << "The result of the expression is: " << (operand1 % operand2) << endl;

break;

}

default:

cout << "ERROR: Invalid operator!\n\n";

}
}
while(input.compare("exit") != 0);


return 0;

}
Last edited on
Where in your code are you asking the user to enter "quit"?

Where in your code are you testing if the user entered "quit"?
Last edited on
I meant to write exit instead of quit.
If I enter "exit" at the first prompt the program exits for me.

A couple of things:

1. PLEASE learn to use code tags, it makes reading and commenting on your source code MUCH easier.

Hint: you can edit your post and add code tags.

http://www.cplusplus.com/articles/jEywvCM9/

2. Post compileable code. That includes headers. Anyone trying to compile your code has to add headers.

3. What happens if the user enters "Exit"? or "EXIT"? You are testing for an exact match with "exit". You need to convert the input to lower case before testing if the user entered exit.

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
#include <iostream>
#include <string>
#include <algorithm> // std::transform
#include <cctype>    // std::::lower

int main()

{
   std::string input { };

   do
   {
      std::cout << "Please enter an expression (enter exit to quit): ";

      std::cin >>input;

      // make the input all lower case
      std::transform(input.begin(), input.end(), input.begin(), std::tolower);

      if (input.compare("exit") == 0)
      {
         std::cout << "\nThank you for using my calculator! Welcome to come back!\n";

         break;
      }

      int operand1      { };
      int operand2      { };
      char charOperator { };

      for (size_t i = 0; i < input.length(); i++)
      {
         if (input[i] == '+' || input[i] == '-' || input[i] == '*' || input[i] == '/' || input[i] == '%')
         {
            operand1 = stoi(input.substr(0, i));

            charOperator = input[i];

            operand2 = stoi(input.substr(i + 1));
         }
      }

      switch (charOperator)
      {
      case '+':
      {
         std::cout << "The result of the expression is: " << (operand1 + operand2) << '\n';
         break;
      }

      case '-':
      {
         std::cout << "The result of the expression is: " << (operand1 - operand2) << '\n';
         break;
      }

      case '*':
      {
         std::cout << "The result of the expression is: " << (operand1 * operand2) << '\n';
         break;
      }

      case '/':
      {
         if (operand2 == 0)
            std::cout << "ERROR: Division by zero!\n";
         else
            std::cout << "The result of the expression is: " << (operand1 / operand2) << '\n';
         break;
      }

      case '%':
      {
         if (operand2 == 0)
            std::cout << "ERROR: Division by zero!\n";
         else
            std::cout << "The result of the expression is: " << (operand1 % operand2) << '\n';
         break;
      }

      default:
         std::cout << "ERROR: Invalid operator!\n\n";

      }
      std::cout << '\n';
   }
   while (input.compare("exit") != 0);
}

Please enter an expression (enter exit to quit): 12/5
The result of the expression is: 2

Please enter an expression (enter exit to quit): 12%5
The result of the expression is: 2

Please enter an expression (enter exit to quit): Exit

Thank you for using my calculator! Welcome to come back!
Topic archived. No new replies allowed.