简体   繁体   中英

C++ cin.fail() while loop

I am trying to make a program that does not accept the code errors when a user puts in something stupid. Such as putting in a string for an integer, I don't know what to do at this point. I am writing random stuff down as it says i don't have enough details even though I have wrote an entire paragraph at this point.

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








}

}

Rather then using such deep nested ugly if-else syntax, you could write this much simpler using the return directive. To avoid duplication you could also return a status and loop until it succeeds.

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

Note the use of system("pause"); is a bad idea also, you really should write your own Press any key to continue functionallity into your program.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM