English 中文(简体)
为什么要成为无限的 lo
原标题:Why does this make an infinite loop
  • 时间:2011-09-27 03:12:43
  •  标签:
  • c++
#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: 
 (r) rectangle 
 (s) square 
 (c) circle 
 (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,  
 );
                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,  
 );
                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;
}

我正试图起草一个方案,要求用户从形状的菜单中提取,然后要求为每一类产品提供某些投入,以确定该地区。 我现在面临的问题是,在人们为rad子或高湿度提出答案时,如何发现错误。 我正试图为试金字书写错误是为了初步不正确的投入而工作,然而,一旦用户再次出现输入错误,就会开始无限循环。

最佳回答

You are using cin.clear() and cin.ignore(1000, ) to skip over the invalid input in the rectangle case, which is good, but you aren t doing that in the other cases.

问题回答

对于我来说,如果我再次打上,(为平方)和,则你的代码将进入 lo。 该法典对投入进行了检查。

#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: 
 (r) rectangle 
 (s) square 
 (c) circle 
 (0) exit" << endl;
        if (!(cin >> input))
        {
            cout << "Break after reading input
";
            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)
";
                break;
            }
            cout << height;
            while (height <= 0)
            {
                cin.clear();
                cin.ignore(1000,  
 );
                cout << "Your input was invalid.  Please input a floating point value greater than 0.";
                if (!(cin >> height))
                {
                    cout << "Break after reading height (2)
";
                    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,  
 );
                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
";
                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
";
                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
";
            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;
}




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签