C++ C++;cin.fail()while循环

C++ C++;cin.fail()while循环,c++,cin,C++,Cin,我试图制作一个程序,当用户输入一些愚蠢的东西时,它不接受代码错误。例如输入一个整数的字符串,此时我不知道该怎么办。我写下随机的东西,因为它说我没有足够的细节,即使我在这一点上写了一整段 void scoretoletter::letter() { int a = 0; int question; while (a == 0) { cout << "1.Rectangle" << endl; cout << "2.Triangle" <&l

我试图制作一个程序,当用户输入一些愚蠢的东西时,它不接受代码错误。例如输入一个整数的字符串,此时我不知道该怎么办。我写下随机的东西,因为它说我没有足够的细节,即使我在这一点上写了一整段

void scoretoletter::letter() {
int a = 0;
int question;
while (a == 0) {
    cout << "1.Rectangle" << endl;
    cout << "2.Triangle" << endl;
    cout << "3.Circle" << endl;
    cout << "4.Exit" << endl;
    float area;
    float Width;
    float Length;
    float Height;
    float base;
    float radius;
    int l;
    cin >> question;
    if (question == 1) {
        cout << "Whats your length?" << endl;
        cin >> Length;
        if (cin.fail()) {
            cout << "That is not valid try again" << endl;
        }
        else {
            cout << "Whats your width?" << endl;
            cin >> Width;
            if (cin.fail()) {
                cout << "That is not valid try again" << endl;

            }
            else {
                if (Length == 0 || Width == 0 ) {
                    cout << "That is not valid try again." << endl;
                    system("pause");
                }

                else {
                    area = Length * Width;
                    cout << "The area is: " << area << endl;
                }
            }
        }
    }

    else if (question == 2) {
        cout << "What is the Base?" << endl;
        cin >> base;
        if (cin.fail()) {
            cout << "That is not valid try again." << endl;


        }
        else {
            cout << "What is the Height?" << endl;
            cin >> Height;
            if (cin.fail()) {
                cout << "That is not valid try again." << endl;

            }
            else {
                if (base == 0 || Height == 0 || cin.fail()) {
                    cout << "That is not valid try again." << endl;
                    system("pause");

                }
                else {
                    area = base * Height * .5;
                    cout << "The area is: " << area << endl;
                }
            }
        }
    }
    else if (question == 3) {
        cout << "What is the radius?" << endl;
        cin >> radius;
        if (radius == 0 || cin.fail()) {
            cout << "That is not valid try again." << endl;
            system("pause");


        }
        else {


            area = radius * radius * 3.14;
            cout << "The area is: " << area << endl;
        }
    }
    else if (question == 4) {
        a = 1;
    }
    else {
        cout << "That is not valid try again." << endl;
    }
    system("pause");








}
void scoretoleter::letter(){
int a=0;
智力问题;
while(a==0){

cout而不是使用这种深度嵌套的丑陋if-else语法,您可以使用
return
指令编写更简单的代码。为了避免重复,您还可以返回状态并循环,直到成功

enum result_status
{
  RESULT_STATUS_OK,
  RESULT_STATUS_INVALID,
  RESULT_STATUS_DONE
}

void scoretoletter::askQuestions() {
  while(true)
    switch(letter())
    {
       RESULT_STATUS_OK:
         system("pause");
         continue;

       RESULT_STATUS_INVALID:
         cout << "That is not valid try again." << endl;
         system("pause");
         cin.clear();             
         continue;

       RESULT_STATUS_DONE:
         system("pause");
         return;
    }
}

enum result_status scoretoletter::letter() {
    int question;

    cout << "1.Rectangle" << endl;
    cout << "2.Triangle" << endl;
    cout << "3.Circle" << endl;
    cout << "4.Exit" << endl;
    float area;
    float Width;
    float Length;
    float Height;
    float base;
    float radius;
    int l;
    cin >> question;

    if (question == 1) {
        cout << "Whats your length?" << endl;
        cin >> Length;
        if (cin.fail())
            return RESULT_STATUS_INVALID;

        cout << "Whats your width?" << endl;
        cin >> Width;
        if (cin.fail() || Length == 0 || Width == 0)
            return RESULT_STATUS_INVALID;

        area = Length * Width;
        cout << "The area is: " << area << endl;
        return RESULT_STATUS_OK;
    }

    if (question == 2) {
        cout << "What is the Base?" << endl;
        cin >> base;
        if (cin.fail())
            return RESULT_STATUS_INVALID;

        cout << "What is the Height?" << endl;
        cin >> Height;
        if (cin.fail())
            return RESULT_STATUS_INVALID;

        if (base == 0 || Height == 0 || cin.fail()) {
            return RESULT_STATUS_INVALID;

        area = base * Height * .5;
        cout << "The area is: " << area << endl;
        return RESULT_STATUS_OK;
    }

    if (question == 3) {
        cout << "What is the radius?" << endl;
        cin >> radius;
        if (radius == 0 || cin.fail()) {
            return RESULT_STATUS_INVALID;

        area = radius * radius * 3.14;
        cout << "The area is: " << area << endl;
        return RESULT_STATUS_OK;
    }

    if (question == 4)
      return RESULT_STATUS_DONE;

    return RESULT_STATUS_INVALID;
}
枚举结果\u状态
{
结果\状态\正常,
结果\状态\无效,
结果\状态\完成
}
void scoretoleter::askQuestions(){
while(true)
开关(字母())
{
结果\状态\正常:
系统(“暂停”);
继续;
结果\状态\无效:

CUT考虑定义一个函数来输入<代码> int <代码>。它可以有一个内部循环,要求用户再试一次。重要的是不要在失败状态下离开<代码> CIN <代码>(因为它忽略了进一步的输入)。,因此,在该函数中,我建议使用
标题中的
getline
,以字符串形式读入一行,然后可能使用
stoi
或任何转换为
int
的方法。或者,可以使用
std::cin.clear()清除错误