C++ cin.fail() while 循环

标签 c++ cin

我正在尝试制作一个程序,当用户输入一些愚蠢的内容时,该程序不接受代码错误。比如将一个整数放入一个字符串,我现在不知道该怎么办。我正在随机写下一些内容,因为它说我没有足够的细节,即使我此时已经写了整个段落。

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");








}

}

最佳答案

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

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;
}

请注意使用system("pause");也是一个坏主意,您确实应该在程序中编写自己的按任意键继续功能。

关于C++ cin.fail() while 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46086077/

相关文章:

c++ - MSVC 与 GCC 与 sizeof 的行为不一致

c++ - std::cin.getline() 与 std::cin

c++ - 为什么我的程序无法识别 C++ 循环内的 Cin?

c++ - 无序映射比 C++ 中的映射慢吗?

c++ - 一个函数可访问的类变量或成员函数中仍然是对象感知的静态变量

c++ - 如何处理 windows.h 中的 max 宏与 std 中的 max 冲突?

c++ - 没有正确处理用户输入

c++ - 如果输入是一个字符然后在 C++ 中存储在一个整数中,为什么 cin 会得到奇怪的值?

C++11:使用成员函数和 this 在对象中启动线程

c++ - 重载括号运算符