Reputation: 65
Trying to write a program that calculates the Volume, Surface Area, or "Girth" of a box. 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 want to end when a Q is typed. I believe my math is right but this program is not working for me. It compiles fine but the Volume wont calculate at all, though the other two subjects will. I also get three lines of answers, and when I enter a second letter for a second value, the program runs crazy. Any and all help is much appreciated. Thanks 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;
}
Upvotes: 3
Views: 16252
Reputation: 311068
Write
while ( cin >> inputChar && inputChar != SENTINEL )
{
//...
instead of
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();
}
Upvotes: 6
Reputation: 11
Just add cin>>inputChar; at the end of while loop. Thats it. Just compile after that & run. You will get expected output. :-)
-Jayesh
Upvotes: 1
Reputation: 4016
You need to prompt 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);
Upvotes: 2
Reputation: 96
Adding to Elliot's answer, I found some improvements in your program without which I don't think it would compile. Like '#' in include statements and wrong switch block. Even for inputting more than one value we need cascading >> but not commas.
Here is a 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;
}
Upvotes: 7
Reputation: 201487
You never update your inputChar
. At the end of your while
loop read another char
,
cin>>inputChar;
}
system ("pause");
Otherwise, it will enter a tight loop (probably with high cpu usage) when the char doesn't match your switch cases or sentinel.
Upvotes: 4