2013-10-03 2 views
-3

Мой код почти закончен, но я не уверен, как вставить цикл while внутри оператора switch, поэтому, если пользователь вводит отрицательное число, он выдает сообщение: «Значение должно быть положительным. Повторите ввод:». Ниже мой код.Как вставить цикл while внутри оператора switch?

#include <iostream> 
#include <iomanip> 
#include <cmath> 
using namespace std; 

const double PI = 3.14; 
void showMenu(); 
double area (double, double); 
double area (double); 
double volume (double, double, double); 
double volume (double); 

int main() 
{ 
char choice; 
double length, width, height, radius, tot_area, tot_volume; 

cout << fixed << setprecision(2); 

do 
{ 
showMenu(); 

cout << "Please select 1-5: " << endl; 
cin >> choice; 

switch (choice) 
{ 
case '1': 
    cout << "Please enter the length of a rectangle: " << endl; 
    cin >> length; 
    cout << "Please enter the width of a rectangle: " << endl; 
    cin >> width; 
    tot_area = area(length, width); 
    cout << "The area of the rectangle is " << tot_area << endl; 
    break; 

case '2': 
    cout << "Please input the radius of the circle: " << endl; 
    cin >> radius; 
    tot_area = area(radius); 
    cout << "The area of the circle is " << tot_area << endl; 
    break; 

case '3': 
    cout << "Please enter the length of a box: " << endl; 
    cin >> length; 
    cout << "Please enter the width of a box: " << endl; 
    cin >> width; 
    cout << "Please enter the height of a box: " << endl; 
    cin >> height; 
    tot_volume = volume(length, width, height); 
    cout << "The volume of the box is " << tot_volume << endl; 
    break; 

case '4': 
    cout << "Please input the radius of a sphere: " << endl; 
    cin >> radius; 
    tot_volume = volume(radius); 
    cout << "The volume of the sphere is " << tot_volume << endl; 
    break; 

case '5': 
    break; 

default : 
     cout << "That is a invalid operation" << endl; 
    break 
} 

} 

while (choice != '5'); 
    cout << "Thank you for using my program." << endl; 

    return 0; 

} 


void showMenu() 
{ 
cout << "1. Calculate the area of a rectangle" << endl; 
cout << "2. Calculate the area of a circle" << endl; 
cout << "3. Calculate the volume of a box" << endl; 
cout << "4. Calculate the volume of a sphere" << endl; 
cout << "5. Quit" << endl; 
} 

//For area of a rectangle 
double area (double length, double width) 
{ 
return length * width; 
} 

//For area of a circle 
double area (double radius) 
{ 
return PI * pow (radius, 2.0); 
} 

//For volume of a box 
double volume (double length, double width, double height) 
{ 
return length * width * height; 
} 

//For volume of a sphere 
double volume (double radius) 
{ 
return (4.0/3.0) * PI * pow (radius, 3.0); 
} 
+0

Если вы хотите петлю где-то там, а затем туда помещают. С какой частью задачи вы столкнулись? Вы попросили вашего инструктора о помощи? Stack Overflow - это не место, чтобы заставить людей делать домашнее задание для вас. –

+0

Спасибо за вашу помощь. Роб оценит это! – user2837593

ответ

0

Вам не нужно вставить while петлю, как вы думаете. Используйте инструкцию continue, если введенный ввод отрицательный.

-2

Вы можете повторить ту же задачу с помощью сделай время:

int flag=0; 
do { 
    cout << "Please enter the length of a rectangle: \n "; 
    cin >> length; 
    cout << "Please enter the width of a rectangle:\n "; 
    cin >> width; 

    if (length<0 || width<0) 
    { 
     flag=1; 
     cout << "please enter the positive number"; 
    } 
} while(flag==1); 
+0

Я пробовал это за последние 30 минут, но так как я новичок здесь, так что не знаю, как писать код, каждый раз он дает код sysntax error на этапе отправки. – user2841164

+0

Я никогда не сталкивался с этой проблемой раньше, поэтому я предполагаю, что это потому, что этот пример лишен отступов. Вы также выбрали код и нажмите CTRL + K, чтобы отформатировать его? – Jamal

+0

Да, я также выбираю ctrl + k и делаю все, что предлагаю, но никогда не было :( – user2841164

Смежные вопросы