Switch statements inside while loop C ++
An attempt to write a program that calculates the volume, surface area, or "girth" of a window. I want to be able to enter a letter for each subject and after it calculates that subject, then enter a letter for the next subject. The program also wants to finish when Q is typed. I believe my math is correct, but this program does not work for me. It compiles fine, but the volume won't compute at all, although the other two items will. I also get three lines of responses, and when I enter the second letter for the second value, the program goes crazy. Any help is greatly appreciated. Thank you for your time.
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;
}
source to share
Adding on to Elliot's answer, I found some improvements in your program without which I don't think this would compile. Like "#" in include statements and wrong toggle block. Even to enter multiple values, we need to cascade -> but not commas.
Here is the code that would compile:
#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;
}
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
}
return 0;
}
source to share
Recording
while ( cin >> inputChar && inputChar != SENTINEL )
{
//...
instead
cin>>inputChar;// pick letter
while (inputChar!=SENTINEL)//
Also you could write
auto prompt = []
{
cout << "\nEnter character V for Volume, character A for Surface Area,\n";
cout << "character G or Girth plus Depth or Q to quit\n"<<endl;
};
while ( prompt(), cin >> inputChar && inputChar != SENTINEL )
{
//...
or
auto prompt = []
{
cout << "\nEnter character V for Volume, character A for Surface Area,\n";
cout << "character G or Girth plus Depth or Q to quit\n"<<endl;
};
prompt();
while ( cin >> inputChar && inputChar != SENTINEL )
{
switch (inputChar)
{
case 'V':
{
//...
}
//...
}
prompt();
}
source to share
You need to ask the user for data again, inside the while loop:
do
{
cout << "Enter character 'V' for Volume, character 'A' for Surface Area,\n";
cout << "character 'G' for Girth plus Depth or 'Q' to quit\n"<<endl;
//Right here before the switch statement
cin >> inputChar;
switch (inputChar)
{
//I also recommend stacking your cases, for lower case and upper case support:
case 'V':
case 'v':
{
//
}
case 'A':
case 'a':
{
//
}
case 'G':
case 'g':
{
//
}
//Add a default case if they're not any of the above letters
default:
{
cout << inputChar << " is not valid input!";
}
}
} while (inputChar != SENTINEL);
source to share