Stopping the program if the user inputs "end"?

I'm currently writing a calculator just for fun. I have a ton of variables that the user inputs so I don't want to write this:

1
2
3
4
5
6

if(a==end || b==end || c==end ||{ //and more and more, you get the point.
cout << "BYE BYE";
return 0;
}


Any way of checking the text from all the variables, so it can be like this instead?

1
2
3
4
if(code for all variables == end){
cout << "BYE BYE";
return 0;
}
Last edited on
You could create a simple menu that asks for user input to decide what operation they would like to use. That way the user can choose to type "End" or some other form.

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

int choice = 0;

cout << " [1] Addition " << endl;
cout << " [2] Subtraction " << endl;
//Add other choices here
cout << "Enter 99999 to exit";

cout << "What would you like to do?" << endl;
cin >> choice;

do
{
     //perform operations here
} (while choice != 99999)
Last edited on
I have a ton of variables that the user inputs so I don't want to write this:


Why are there lots of variables? If there are lots then the design is probably wrong. The psuedo code for a calculator is this:


1
2
3
4
5
//show the menu - a function
//get a number & validate it- a function
//get an operator  & validate it- a function
//get a number - call this function again
//calc & print out the answer a function 


You can make use of a while loop with a switch inside:

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
bool Quit = false;

while (!Quit) {
// ....

    switch (operator) {
         case '1':
             //addition
         break;

         case '2':
             //subtraction
         break;

         case '3':
             //multiplication
         break;

         case '4':
             //division - check for divide by zero
         break;

         case '5':
             //user wants to quit
             Quit = true;
         break;

         default:
              //bad input
         break;

    }
}


If you have any problems - then post your code & any compilation errors in full.

Hope all goes well.

Edit:

@CodingKid

IMO, the switch is much better than a do loop.
Last edited on
You could create a simple menu that asks for user input to decide what operation they would like to use. That way the user can choose to type "End" or some other form.


I did a very similar thing.

1
2
3

cout << endl << "Basic math = bm, Geometry = g, Square root = s. Power = p.";


However, lets say you are doing addition

Enter a number: 4
What do you want to add to 4? *Screw this.*

I want the user to be able to type end, and stop the program.


@TheIdeasMan

Its not just addition, its calculating area of shapes, subtraction, multiplication and division.
I could type out the 13 variables, but I want to know if there is a more effective way.
Last edited on
IMO, the switch is much better than a do loop.


You can easily convert a do while loop to a switch statement.
Really need some help :(

Is it possible to call all variables?
Its not just addition, its calculating area of shapes, subtraction, multiplication and division.
I could type out the 13 variables, but I want to know if there is a more effective way.


That doesn't matter, you can still put them in a switch - you can have as many operator as you like. IMO the switch is the best way.
Topic archived. No new replies allowed.