#include <iostream>
#include <math.h>
using namespace std;
int main() {
double radius = 0.00;
double height = 0.00;
double width = 0.00;
double side = 0.00;
double Area = 0.00;
const double PI = atan(1.0)*4;
string input = "none";
while (input != "0") {
cout << "Shape Menu: \n (r) rectangle \n (s) square \n (c) circle \n (0) exit" << endl;
cin >> input;
if (input == "r"){
cout << "Please enter a floating point value for the height of the rectangle." << endl;
cin >> height;
cout << height;
while (height <= 0)
{
cin.clear();
cin.ignore(1000, '\n');
cout << "Your input was invalid. Please input a floating point value greater than 0.";
cin >> height;
}
cout << "Please enter a floating point value greater than 0 for the width of the rectangle." << endl;
cin >> width;
while (width <= 0)
{
cin.clear();
cin.ignore(1000, '\n');
cout << "Your input was invalid";
cin >> width;
}
Area = height*width;
cout << "The area of a rectangle with those dimensions is " << Area << "units squared." << endl;
}
else if(input == "s"){
cout << "Please enter a floating point value for the length of the side." << endl;
cin >> side;
if (cin.fail())
cout << "Please enter a floating point value.";
Area = side*side;
cout << "The area of a square with those dimensions is " << Area << "units squared" << endl;
}
else if(input == "c"){
cout << "Please enter a floating point value for the length of the radius." << endl;
cin >> radius;
if (cin.fail())
cout << "Please enter a floating point value.";
Area = radius*radius*PI;
cout << "The area of a circle with those dimensions is " << Area << "units squared." << endl;
}
else if(input == "0"){
break;
}
else{
cout << "Your input does not match one of the options suggested. Please type r, s, c to get the area of a shape, or type 0 to exit." << endl;
}
}
return 0;
}
我正在尝试编写一个程序,要求用户从形状菜单中选择,然后要求每种类型的某些输入来确定区域。我现在遇到的问题是,当人们输入半径或高度和宽度不是数字的答案时,试图弄清楚如何引发错误。我试图为矩形编写的错误适用于初始错误输入,但是一旦提示用户选择另一个形状,如果再次发生输入错误,则它开始无限循环。
答案 0 :(得分:0)
您正在使用cin.clear()和cin.ignore(1000,'\ n')跳过矩形大小写中的无效输入,这很好,但在其他情况下您不会这样做。
答案 1 :(得分:0)
对我来说,如果我再次键入 s (对于square)和 s ,您的代码将进入循环。这段代码没有;它会检查输入:
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
double radius = 0.00;
double height = 0.00;
double width = 0.00;
double side = 0.00;
double Area = 0.00;
const double PI = atan(1.0)*4;
string input = "none";
while (input != "0")
{
cout << "Shape Menu: \n (r) rectangle \n (s) square \n (c) circle \n (0) exit" << endl;
if (!(cin >> input))
{
cout << "Break after reading input\n";
break;
}
cout << "Input: <<" << input << ">>" << endl;
if (input == "r")
{
cout << "Please enter a floating point value for the height of the rectangle." << endl;
if (!(cin >> height))
{
cout << "Break after reading height (1)\n";
break;
}
cout << height;
while (height <= 0)
{
cin.clear();
cin.ignore(1000, '\n');
cout << "Your input was invalid. Please input a floating point value greater than 0.";
if (!(cin >> height))
{
cout << "Break after reading height (2)\n";
break;
}
}
cout << "Please enter a floating point value greater than 0 for the width of the rectangle." << endl;
if (!(cin >> width))
break;
while (width <= 0)
{
cin.clear();
cin.ignore(1000, '\n');
cout << "Your input was invalid";
if (!(cin >> width))
break;
}
Area = height*width;
cout << "The area of a rectangle with those dimensions is " << Area << " units squared." << endl;
}
else if (input == "s")
{
cout << "Please enter a floating point value for the length of the side." << endl;
if (!(cin >> side))
{
cout << "Break after reading length\n";
break;
}
if (cin.fail())
cout << "Please enter a floating point value.";
Area = side*side;
cout << "The area of a square with those dimensions is " << Area << " units squared" << endl;
}
else if (input == "c")
{
cout << "Please enter a floating point value for the length of the radius." << endl;
if (!(cin >> radius))
{
cout << "Break after reading radius\n";
break;
}
if (cin.fail())
cout << "Please enter a floating point value.";
Area = radius*radius*PI;
cout << "The area of a circle with those dimensions is " << Area << " units squared." << endl;
}
else if (input == "0")
{
cout << "Break after reading zero\n";
break;
}
else
{
cout << "Your input does not match one of the options suggested.\n"
<< "Please type r, s, c to get the area of a shape, or type 0 to exit." << endl;
}
}
return 0;
}