2013-10-03 52 views
-3

我的代碼幾乎完成,但我不知道如何在switch語句中插入一個while循環,所以如果用戶輸入一個負數,它會給出一條消息,「該值必須是正數,請重新輸入:」。以下是我的代碼。如何在switch語句內插入while循環?

#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

如果你想有一個循環的地方在那裏,然後把一個有。你遇到什麼困難?你是否問過你的導師幫忙?堆棧溢出不是讓人們爲你做功課的地方。 –

+0

感謝您的幫助羅布欣賞它! – user2837593

回答

0

您不必按照您的想法插入while循環。如果輸入的輸入是負數,則使用continue聲明。

-2

您可以重複相同的任務與DO-同時的幫助:

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錯誤。 – user2841164

+0

我從來沒有遇到過這個問題,所以我認爲這是因爲這個例子沒有縮進。你是否也選擇了代碼並按下CTRL + K進行格式化? – Jamal

+0

是的,我也選擇CTRL + K,並做了所有的建議,但從來沒有登上:( – user2841164