switch statement with basic math problem

Oct 17, 2017 at 5:59pm
The issue i am having is initializing the math problem. cannot use string or if. must be able to just put in a simple math problem 8+8 and it will display the answer 16
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
#include <iostream>
#include <cassert>
#include <cmath>

using namespace std;

int main()
{
     char a, b;
     float x, y, z;

     cout << "1. Simple math expression evaluation." << endl;
     cout << "2. Exit. " << endl;
     cout << "Select an option: ";
     cin >> a;
     cout << endl;

     switch(a)
     {
          case '1':
               cout << "Enter a simple math expression: " << endl;
               cin >> b;

               switch(b)
               {
               case '1': (x + y == z);
                    cout << x << " + " << y << " = " << x + y;
                    cout << z << endl;
                    break;
               }
          default:
               cout << "Goodbye!" << endl;
     }

     return (0);
}
Oct 17, 2017 at 6:37pm
not sure exactly what you want, but you need to either read in or assign x,y, and z. All 3 are being used uninitialized.

Oct 17, 2017 at 6:39pm
What is the user expected to type?
((42 + 7 )*( 8/4))/ 7

OR
n o m

where n and m are integers and o is one of the operators: + - / *
Oct 17, 2017 at 6:41pm
On line 26, you have (x + y == z);
This isn't doing anything useful. Nothing. Nada. It is making a comparison of the addition of two uninitialized variables to a third, uninitialized variable.

By the way, variable names like "a" and "b" are very unclear names.

x, y, and z are all still uninitialized when you try to display them.

I'll start you off with this:
1
2
3
4
5
6
7
8
9
10
11
12
13
float x, y, z;
char math_operator;
cout << "Enter a simple math expression (ex: 3 + 2): " << endl;
cin >> x >> math_operator >> y;
switch (math_operator)
{
    case '+':
        z = x + y;
        // ...
        break;

    //case default: // etc.
}
Last edited on Oct 17, 2017 at 6:46pm
Oct 17, 2017 at 11:22pm
thank you. i was having problem with initializing into the nested switch or getting the math problem to work.
Oct 18, 2017 at 12:09am
the issue i am having now is that no answer is displaying when i do the math part.


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

#include <iostream>
#include <cassert>

using namespace std;

int main()
{
     char option_1, math_operator;
     float x, y, z;

     cout << "1. Simple math expression evaluation." << endl;
     cout << "2. Exit. " << endl;
     cout << "Select an option: ";
     cin >> option_1;
     cout << endl;

     switch (option_1)
     {
          case '1':
               cout << "Enter a simple math expression: " << endl;
               cin >> x >> math_operator >> y;
               break;
          case '2':
               cout << "Goodbye!" << endl;
               break;

               switch (math_operator)
               {
               case '+':
                    z = x + y;
                    cout << z << endl;
                    break;
               case '-':
                    z = x - y;
                    cout << z << endl;
                    break;
               case '*':
                    z = x * y;
                    cout << z << endl;
                    break;
               case '/':
                    assert(y);
                    z = x / y;
                    cout << z << endl;
               }
          default:
               cout << "Invalid choice!" << endl;
     }

     return (0);
}
Oct 18, 2017 at 12:18am
closed account (DoLCX9L8)
Your second switch is floating in vacuum.

Clearly, the switch at line 28 should be after line 22 (inside case '1').
Topic archived. No new replies allowed.