#Beginner here! Tackling a problem with error handling.

8 messages · Page 1 of 1 (latest)

tropic geodeBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

sharp onyx
#

now the problem here is in this error handling

#
{
    int choice = 1;
    int createChoice = 1;

    do
    {
        cout << "choose" << endl;
        cout << "1. create" << endl;
        cout << "2. update" << endl;
        cout << "0. exit" << endl;
        cin >> choice;

        if (choice != 1 || choice != 2 || choice != 0){
            cout << "Invalid try again" << endl;
         }
        else if (cin.fail()){
            cin.clear();
            cin.ignore();
            cout << "Invalid try again" << endl;
            continue;
        }
        switch (choice)
        {
        case 1:
            create(createChoice); 
        break;
        case 2:
            update();
        break;
        }
    } while (choice != 0);

    return 0;
}```
#

When i input a letter in cin >> choice

shouldnt it say "Invalid try again" and loop back to the menu? however it just stops at saying "Invalid try again"

tropic geodeBOT
#

@sharp onyx

Please Do Not Delete Posts!

Please don't delete forum posts. They can be helpful to refer to later and other members can learn from them. In the future you can use !solved to close a post and mark a post as solved.

sharp onyx
#

fixed it by if

            cin.clear();
            cin.ignore(9999, '\n');
            cout << "Invalid try again" << endl;
            choice = -1;
            continue;```
#

!solved