Help with switch statements inside while a loop

Trying to write a program that can calculate the volume, surface area, or "girth" of a box. I want to be able to enter a letter for each subject and after it calculate that subject enter a letter for the next subject. Also want the program to end when a Q is typed. I believe my math is right but this sucker wont work for me. It compiles fine but the Volume wont calculate at all though the other two will. I also get three lines of answers, and when I go to enter a second letter for a second value the program runs like crazy. Any and all help is much appreciated. Thanks for your time.

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

using namespace std;

const char SENTINEL = 'Q';//found this suggestion online to quit a program

int main ()
{
   
    char V, A, G, inputChar;
    float length, width, depth, totalV, totalA, totalG;
    
    cout<<"Enter character V for Volume, character A for Surface Area,\n";
    cout<<"character G or Girth plus Depth or Q to quit\n"<<endl;
    cin>>inputChar;// pick letter
    while (inputChar!=SENTINEL)// if not Q then check the if-ele statments
    {
          switch (inputChar)
          case 'V':
          {
                    cout<<"Enter Length, Width, and Depth for Volume\n";
                    cin>>length, width, depth;
                    totalV=length*width*depth;//math for volume
                    cout<<"The Volume = "<<totalV<<endl;
                    break;
          }
          case 'A':
          {
                   cout<<"Enter Length, Width, and Depth for Surface Area\n";
                   cin>>length, width, depth;
                   totalA=2*length*width+2*width*depth+2*length*depth;//math for area
                   cout<<"The Surface Area = "<<totalA<<endl;
                   break;
          }
          case 'G':
          {
                   cout<<"Enter Length, Width, and Depth for Girth\n";
                   cin>>length, width, depth;
                   totalG=2*(length+width)+depth;//math for girth
                   cout<<"The Girth = "<<totalG<<endl;
                   break;
          }          
    }
          system ("pause");
          return 0;
}
The reason why the program goes "haywire" is that cin<< isn't called anymore within the while loop.
closed account (SECMoG1T)
Hi
cin>>length, width, depth; is an error replace with cin>> length>> width>> depth;

You might consider a default case too.
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
cin>> inputChar;
 while (inputChar!=SENTINEL)// if not Q then check the if-ele statments 
      { 
          switch (inputChar) 
             {
                case 'V': 
                          { 
                               cout<<"Enter Length, Width, and Depth for Volume\n"; 
                               cin>>length>>width>>depth; 
                               totalV=length*width*depth;//math for volume 
                               cout<<"The Volume = "<<totalV<<endl; 
                               break;
                          } 
              case 'A':
                          { 
                               cout<<"Enter Length, Width, and Depth for Surface Area\n"; 
                               cin>>length>> width>>epth; 
                               totalA=2*length*width+2*width*depth+2*length*depth;//math for area 
                               cout<<"The Surface Area = "<<totalA<<endl;
                               break;
                          } 
               case 'G':
                          { 
                                cout<<"Enter Length, Width, and Depth for Girth\n"; 
                                cin>>length>> width>>depth; 
                                totalG=2*(length+width)+depth;//math for girth 
                                cout<<"The Girth = "<<totalG<<endl;
                                break; 
                          } 
              default:  
                          {
                                cout <<"error wrong entry\n";  break; 
                          }
            }
    
            cin>> inputChar; 
     }


Note you need to put your switch into a block lile i have done.
Last edited on
http://www.cplusplus.com/doc/tutorial/operators/ (comma operator)

1
2
//cin>>length, width, depth;
cin>>length>>width>>depth;
Thank you so much for the help. :)
Topic archived. No new replies allowed.