2011-09-27 72 views
1
#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; 
} 

我想写一个程序,要求用户从形状菜单中选择,然后要求为每个类型确定该区域的某些输入。我现在遇到的问题是试图弄清楚如何在人们输入半径或非数字的高度和宽度的答案时引发错误。我试图为矩形写入的错误对于初始不正确的输入有效,但是一旦用户被提示,就选择另一个形状,如果再次出现输入错误,则会开始无限循环。这是什么使一个无限循环

+2

我无法重现你的错误,你的代码确实是令人费解的。也许只是给出导致错误而不是口头描述的确切输入。还要检查从cin读取的if(cin >> foo)是否成功。 – pmr

回答

0

您正在使用cin.clear()和cin.ignore(1000,'\ n')跳过矩形案例中的无效输入,这很好,但在其他情况下您没有这样做。

0

对我来说,如果我再次输入s(对于正方形)和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; 
}