Home > database >  Switch Case with While Loop in C Menu
Switch Case with While Loop in C Menu

Time:06-03

I've been building a menu driven console in C , and I'm currently using switch-case as my options, but now I'm stuck in switch case.

Here's the scenario:

SCENARIO

Explanation: After inputting invalid option in the main menu, it gives an error which prompts the user to re-input their desired option, now my problem is when the user inputs the correct option for the 2nd attempt, it loops back to the main menu instead of redirecting it to the next menu.

My Goal: To go to the 2nd menu directly from the default without redisplaying the main menu.

My Partial Code:

#include <iostream>
#include <string>
#include <stdlib.h>

using namespace std;
int choice;
int booknumber;
int booktitle;
int author;
int datepublished;
int e = 0;

void menu();
void inputbook();
void searchbook();
void borrowbook();
void exit();

//CLASS
class Books
{
    public:
        int booknumber;
        string booktitle;
        string author;
        string datepublished;
        Books(const int booknumber, const string booktitle, const string author, const string datepublished) : booknumber(booknumber), booktitle(booktitle), author(author), datepublished(datepublished) {}
};

//MAIN
int main()
{ 
    while (true)
    {
        cout << endl;
        if (e == 1)
        {
            break;
        }
        menu ();
    }

    return 0;    
}

//MENU
void menu()
{
    cout << "Welcome to DLC Library System\n";
    cout << "Final Project in Advance Programming\n\n";

    cout << "PROGRAMMER\n";
    cout << "ME\n\n";

    cout << "====================================\n";
    cout << "[1] -------- Input Book ------------\n";
    cout << "[2] -------- Search Book -----------\n";
    cout << "[3] -------- Borrow Book -----------\n";
    cout << "[4] -------- Exit Program ----------\n";
    cout << "====================================\n";
    cout << "Input your choice (Number Only): ";
    cin >> choice;

    switch (choice)
    {
    case 1:
        inputbook ();
        break;
    case 2:
        searchbook ();
        break;
    case 3:
        borrowbook ();
        break;
    case 4:
        exit();
        break;
    default:
        while (choice < 1 || choice > 4)
        {
            cout << "Wrong Option\n";
            cout << "Input your choice (Number Only): ";
            cin >> choice;

                if (choice < 1 || choice > 4)
            {
                continue;
            }
        }
    }
    
    
}

// INPUT BOOK
void inputbook ()
{
    int booknumber;
    string booktitle;
    string author;
    string datepublished;

    cout << "INPUT NEW BOOK\n\n";

    cout << "Book Number: \n";
    cin >> booknumber;

    cout << "Book Title: \n";
    cin >> booktitle;

    cout << "Author: \n";
    cin >> author;

    cout << "Date Publish: \n";
    cin >> datepublished;
    Books(booknumber,booktitle, author, datepublished);
    
    cout << "====================================\n";
    cout << "[1] -------- Try Again? ------------\n";
    cout << "[2] -------- Return to Menu --------\n";
    cout << "[3] -------- Exit Program ----------\n";
    cout << "====================================\n";
    cout << "Input your choice (Number Only): ";
    cin >> choice;

    switch (choice)
    {
    case 1:
        inputbook ();
        break;
    case 2:
        menu ();
        break;
    case 3:
        exit();
    default:
        cout << "Wrong Option";
    }
}

CodePudding user response:

It's a good idea to avoid repeating code. Here, you have a default case that is essentially an input loop, whereas you could have done that input loop at the start. So the way you wrote it, you still need a loop around the whole thing, plus more logic which makes the code harder to read and more bug-prone.

Why not simply:

cout << "====================================\n";
cout << "[1] -------- Input Book ------------\n";
cout << "[2] -------- Search Book -----------\n";
cout << "[3] -------- Borrow Book -----------\n";
cout << "[4] -------- Exit Program ----------\n";
cout << "====================================\n";

int choice;
bool validInput = false;

while (!validInput)
{
    cout << "Input your choice (Number Only): ";
    if (!(cin >> choice)) {
        std::cerr << "Aborted\n";
        return;
    }

    validInput = (choice >= 1 && choice <= 4);
    if (!validInput) {
        std::cout << "Invalid input\n";
    }
}

switch(choice)
{
    // ...
}

Now it's up to you to make your input routine more robust if you choose. Notice I've already bailed out of the function if the input fails. That could be from a stream error, but it could also be if the user enters a non-integer value.

You may instead wish to read your input as a string using std::getline and then convert that to an integer with std::stoi or parse the value from a std::istringstream.

CodePudding user response:

Instead

continue;

try calling

inputbook();

so it won't go back. That's a problem you are facing because you called switch-case again which is "continue". That's why it goes back to the menu when the user inputs the acceptable range of int you just set.

CodePudding user response:

Just modify the code as below, and handle the valid input verification before entering the switch, in this way you can simply mitigate the issue you had!

cin >> choice;
while (choice < 1 || choice > 4)
    {
        cout << "Wrong Option\n";
        cout << "Input your choice (Number Only): ";
        cin >> choice;

            if (choice < 1 || choice > 4)
        {
            continue;
        }
    }

switch (choice)
{
case 1:
    inputbook ();
    break;
case 2:
    searchbook ();
    break;
case 3:
    borrowbook ();
    break;
default:
    exit();
    break;
}
  • Related