Switch structures

Hello, I want to change my if/else structure to a switch structure. However, I'm not entirely sure how to go about doing so. Any help is appreciated!

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
#include <iostream.h>
#include <iomanip.h>

           
   int main()
           
   
   {
   
      double ans, num1, num2;
      int choice = 1;
   
      while(choice)
      {
         cout<<"\n\n1) Add\n2) Subtract\n3) Multiply\n4) Divide\n5) Quit\n\n";
         cout<<"What operation would you like to do?: ";
      
         for(cin>>choice;choice<1 || choice>5;cin>>choice)
         {
            cout<<"\n\n1) Add\n2) Subtract\n3) Multiply\n4) Divide\n5) Quit\n\n";
            cout<<"What operation would you like to do?: ";
         }
         if (choice>0 && choice<5)
         {
            cout<<"\n\nEnter the first number: ";
            cin>>num1;
            cout<<"\n\nEnter the second number: ";
            cin>>num2;
            cout<<"\n------------------------------------\n";
         }
         if (choice==1)
         {
            ans=num1+num2;
            cout<<"\nThe sum of "<<num1<<" and "<<num2<<" is "<<ans<<endl;
         }
         else if (choice==2)//subtraction
         {
            ans=num1-num2;
            cout<<"\nThe difference of "<<num1<<" and "<<num2<<" is "<< ans<<endl;
         }
         else if (choice==3)//multiplication
         {
            ans=num1*num2;
            cout<<"\nThe product of " <<num1<< " and " << num2 << " is " << ans << endl;
         }
         else if (choice==4) //division
         {
            if (num2==0)
            {
               cout<<"\nThe quotient of  "<<num1<<" and "<<num2<< " is undefined."<<endl;
            }
            else
            {
               ans=num1/num2;
               cout<<"\nThe quotient of " <<num1<<" and "<<num2<<" is "<<ans<<endl;
            }
         }
         else if (choice==5)
         {
            choice=0;  //quiting the while loop
            break;
         }
         cout<<"\n------------------------------------\n";
      
      }
   
      cout<<"\n**************************************";
      cout<<"\n\nThank you for using the calculator.";
      return 0;
   }




This is what I've attempted so far, but not having any luck:

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
#include <iostream.h>
#include <iomanip.h>


           
   int main()
           
   
   
   {
   
      double ans, num1, num2;
      int choice = 1;
   
      while(choice)
      {
         cout<<"\n\n1) Add\n2) Subtract\n3) Multiply\n4) Divide\n5) Quit\n\n";
         cout<<"What operation would you like to do?: ";
      
         for(cin>>choice;choice<1 || choice>5;cin>>choice)
         {
            cout<<"\n\n1) Add\n2) Subtract\n3) Multiply\n4) Divide\n5) Quit\n\n";
            cout<<"What operation would you like to do?: ";
         }
         if (choice>0 && choice<5)
         {
            cout<<"\n\nEnter the first number: ";
            cin>>num1;
            cout<<"\n\nEnter the second number: ";
            cin>>num2;
            cout<<"\n------------------------------------\n";
         }
         switch (choice){
            case '1':
            
               ans=num1+num2;
               cout<<"\nThe sum of "<<num1<<" and "<<num2<<" is "<<ans<<endl;
               break;
         
            case '2': //subtraction
            
               ans=num1-num2;
               cout<<"\nThe difference of "<<num1<<" and "<<num2<<" is "<< ans<<endl;
               break;
         
            case '3': //multiplication
            
               ans=num1*num2;
               cout<<"\nThe product of " <<num1<< " and " << num2 << " is " << ans << endl;
               break;
         
            case '4': //division
            
               if (num2==0)
               {
                  cout<<"\nThe quotient of  "<<num1<<" and "<<num2<< " is undefined."<<endl;
               }
               else
               {
                  ans=num1/num2;
                  cout<<"\nThe quotient of " <<num1<<" and "<<num2<<" is "<<ans<<endl;
               }
               break;
         
            case '5': 
               choice=0;  //quiting the while loop
               break;
         
         
         
         }
         cout<<"\n------------------------------------\n";
         cout<<"\n**************************************";
         cout<<"\n\nThank you for using the calculator.";
         return 0;
      }
   }
Last edited on
One problem is that your variable choice is an integer which is correct. Your case statements are looking for a character. Just take off the single quotes and you'll have better luck.
Last edited on
/facepalm

Thanks @Kooth, I appreciate it.
Topic archived. No new replies allowed.