While Loop + Switch Statements

Hey guys, I am making a program to take in math functions and a value. Here is my code, all of the cases in the switch statements works fine the only problem is when the user want to exit the program. When the user input X as the function, case 'X' doesn't output the cout statement.



#include <iostream>
#include <cmath>

using namespace std;


int main()
{
double theSine, theCosine, theTangent, theSqrt, theNatlog;
double num;
char function;

cout << "This calculator requires you to enter a function and a number " << endl;
cout << "The function are as follows: " << endl;
cout << "S - Sine" << endl;
cout << "C - Cosine" << endl;
cout << "T - Tangent" << endl;
cout << "R - Square Root" << endl;
cout << "N - Natural Log" << endl;
cout << "X - to exit the program" << endl;
cout << endl;

cout << "Please enter a function and a value " << endl;
cin >> function >> num;

while (function != 'X')
{
switch (function)
{
case 'S':
theSine = sin(num);
cout << "The sine of your number is " << theSine << endl;
break;
case 'C':
theCosine = cos(num);
cout << "The cosine of your number is " << theCosine << endl;
break;
case 'T':
theTangent = tan(num);
cout << "The tangent of your number is " << theTangent << endl;
break;
case 'R':
theSqrt = sqrt(num);
cout << "The square root of your number is " << theSqrt << endl;
break;
case 'N':
theNatlog = log(num);
cout << "The natural log of your number is " << theNatlog << endl;
break;
case 'X':
cout << "Thank you for using the calculator " << endl;
break;
default:
cout << "Invalid function" << endl;
}
cout << "Please enter a function and a value " << endl;
cin >> function >> num;
}

return 0;
}

The while loop controls access to the switch statement. The while loop condition is while (function != 'X') -- if they've entered 'X', the code won't go into the switch to use the 'X' case statement.

A couple of other thoughts...

If they do enter 'X' as the function to exit, they still need to enter a value before the program actually exits.

Since they can use the calculator multiple times, it might be helpful to redisplay the menu of possible choices after a calculation is completed.
Thanks for the idea. I added an if statement before the loop starts and after the loop so they can exit the program so that when they enter the function 'X' it would output the cout statement. Maybe I can redisplay the choices like "or press X followed by a 0" or something, but I'll think of a way to just accept the function 'X' without the value.
Last edited on
... but I'll think of a way to just accept the function 'X' without the value.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
bool Quit = false;

while (!Quit) {
  ShowMenu();
// get Menu Selection
   if (MenuSelection != 'Q') {
       // input number
   }
   switch (MenuSelection) {
   /// ...
   case 'Q' :
     Quit = true;
     std::cout << "Thank you for using the calculator \n";
     break;

    default:
    // ....
   }

}


Please always use code tags. Select your code then press the <> button on the format menu.
http://www.cplusplus.com/articles/z13hAqkS/
closed account (48T7M4Gy)
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
#include <cmath>
#include <iostream>

using namespace std;


int main()
{
    double theSine, theCosine, theTangent, theSqrt, theNatlog;
    double num;
    char function;
    
    cout << "This calculator requires you to enter a function and a number " << endl;
    cout << "The function are as follows: " << endl;
    cout << "S - Sine" << endl;
    cout << "C - Cosine" << endl;
    cout << "T - Tangent" << endl;
    cout << "R - Square Root" << endl;
    cout << "N - Natural Log" << endl;
    cout << "X - to exit the program" << endl;
    cout << endl;
    
    while (cout << "Please enter a function: "
           && cin >> function && toupper(function) != 'X')
    {
        cout << "Please enter a number: ";
        cin >> num;
        
        switch ( toupper(function) )
        {
            case 'S':
                theSine = sin(num);
                cout << "The sine of your number is " << theSine << endl;
                break;
            case 'C':
                theCosine = cos(num);
                cout << "The cosine of your number is " << theCosine << endl;
                break;
            case 'T':
                theTangent = tan(num);
                cout << "The tangent of your number is " << theTangent << endl;
                break;
            case 'R':
                theSqrt = sqrt(num);
                cout << "The square root of your number is " << theSqrt << endl;
                break;
            case 'N':
                theNatlog = log(num);
                cout << "The natural log of your number is " << theNatlog << endl;
                break;
            default:
                cout << "Invalid function" << endl;
        }
    }
    
    cout << "Thank you for using the calculator " << endl;
    
    return 0;
}
Topic archived. No new replies allowed.