Program with switch and sentinel loop

Hello,

A beginner hoping someone could point me in the right direction with regards to this program. I'm getting confused when working with a sentinel loop. This is homework for me, so that is why I'm not looking for someone to give me the complete solution.

This is what I currently have. Thank you for whatever help you may give

#include <iostream>
#include <iomanip>

using namespace std;

const int q = -1;

int main ()
{
char option = ' ';
float volume, area, girth, length, width, depth;

cout << "To determine Volume, enter V \n"
<< "To determine Surface Area, enter A \n"
<< "To determine Girth, enter G \n"
<< "To quit, enter Q\n";
cin >> option;

cout << "Please input the length: ";
cin >> length;
cout << "Please input the width: ";
cin >> width;
cout << "Please input the depth: ";
cin >> depth;

while (option != q)
{


switch (option)
{
case 'V':
volume = length * width * depth;
cout << "The volume of the object is: " << volume << endl;
break;
case 'A':
area = 2 *(length * width) + 2 *(width * depth) + 2 *(length * depth);
cout << "The surface area of the object is: " << area << endl;
break;
case 'G':
girth = 2 * (length + width) + depth;
cout << "The girth of the object is: " << girth << endl;
break;
}


system("pause");
return 0;
}
Any errors?
Last edited on
Should you fix it like below?

from:
const int q = -1;
to:
const char q = 'Q';

move:
while (option != q)
{
right after:
float volume, area, girth, length, width, depth;
closed account (zb0S216C)
Yooper wrote:
This is homework for me, so that is why I'm not looking for someone to give me the complete solution. (sic)

Finally! Only if all newcomers were like you.

I've noticed something here. The while loop is set to continue while option has a value other than negative one; that's fine. However, option is never given the chance to change its value, via std::cin. Without input, the while loop will never be false, thus, the loop will forever continue. Place a std::cin statement within the while loop and place the given value within option. I'll let you decide where in the loop the statement should go.

Wazzak
Last edited on
Thank You EricDu and Framework for your help
Topic archived. No new replies allowed.