问题描述
我正在尝试创建一个用于输入验证的循环,如果用户要退出,则尝试创建另一个循环。还可以创建四个功能,一个用于菜单,一个用于三个区域计算中的每一个。
我应该在哪里实现更新的代码?我应该使用新变量还是利用旧变量。谢谢你
到目前为止,这是我的代码:
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
const float PI = 3.14159;
int user_choice;
// main screen prompting for choice
cout << "\nGeometry Calculator\n"
<< " 1. Calculate the Area of a Circle\n"
<< " 2. Calculate the Area of a Rectangle\n"
<< " 3. Calculate the Area of a Triangle\n"
<< " 4. Quit\n"
<< "\nEnter you choice (1-4): ";
cin >> user_choice;
cout << endl;
switch (user_choice)
{
float area;
// if user chooses 1
case 1:
int radius;
// prompt user for radius
cout << "What is the radius: ";
cin >> radius;
// if user enters radius as negative number
if (radius < 0)
{
cout << "\nThe radius must be a positive number.\n"
<< "Try again.\n"
<< endl;
}
else
// calculate area
{
area = PI * pow(radius,2);
cout << "The area of the circle is "
<< area << endl;
}
break;
// if user chooses 2
case 2:
float width,length;
// prompt user for height and width
cout << "What is the length? " << endl;
cin >> length;
if (length > 0)
{
cout << "What is the width? " << endl;
cin >> width;
// calculate and display area
if (width > 0) {
area = length * width;
cout << "The area of rectangle is "
<< area
<< endl;
}
// if user enters a height or width less than 0
else {
cout << "\nWidth must be a positive number"
<< endl;
cout << "Try again." << endl;
}
}
else
{
cout << "\nLength must be a positive number" << endl;
cout << "Try again." << endl;
}
break;
// if user chooses 3
case 3:
float height,base;
// prompt user for base and height
cout << "What is the base? ";
cin >> base;
if (base > 0)
{
cout << "What is the height? ";
cin >> height;
// calculate and display area
if (height > 0)
{
area = (base * height) * .5;
cout << "Area of triangle is "
<< area
<< endl;
}
// if user inputs a height or base less than 0
else
{
cout << "\nHeight must be a positive number\n"
<< "Try again."
<< endl;
}
}
else
{
cout << "\nBase length must be a positive number\n"
<< "Try again." << endl;
}
break;
// if user chooses 4
case 4:
cout << "You have chose to quit the program. Good-bye." << endl;
break;
// if user enters input other than 1-4
default:
cout << "\nYour choice must be between 1 and 4.\n"
<< "Try again."
<< endl;
break;
}
解决方法
如果我了解您想要的内容,则应创建一个while(true)
循环,其中包括用户的主要选择(主菜单)。我会做这样的事情:
void calculateCircleArea() {
// Circle area calculation
}
void calculateRectangleArea() {
// Rectangle area calculation
}
void calculateTriangleArea() {
// Triangle area calculation
}
int main()
{
// other main stuff
int user_choice=0;
while (user_choice!=4)
{
cout << "\nGeometry Calculator\n"
<< " 1. Calculate the Area of a Circle\n"
<< " 2. Calculate the Area of a Rectangle\n"
<< " 3. Calculate the Area of a Triangle\n"
<< " 4. Quit\n"
<< "\nEnter you choice (1-4): ";
cin >> user_choice;
if (user_choice<1 || user_choice>4) // example to inform the user of a bad input
{
cout << "Wrong input\n";
cin.clear();
cin.ignore(INTMAX_MAX,'\n');
// Ignoring and clearing all the input until the delimiter character ('\n')
continue; // it is unnecessary,but this keyword will skip the cycle in which it is to its next iteration
}
switch(user_choice)
{
case 1:
calculateCircleArea();
break;
case 2:
calculateRectangleArea();
break;
case 3:
calculateTriangleArea();
break;
case 4:
// nothing,because the value of the variable user_choice is 4,so the loop will end
break;
default:
// default stuff
break;
} // switch
} // while
} // main
如您所见,我包含了三个函数,每个计算一个。我建议您尝试更多地使用函数,这将使您的代码更具可读性和简洁性,而它们又由于您将要学习的其他原因而有用。
编辑:按照建议,我没有想到要输入字符,因此只要输入错误,输入缓冲区就会被清除,并且它将忽略最后插入的行,以防止意外行为。