0

Trying to make a menu but make it accept only integer for selecting option and loop back when user inputs letter.

AppUI.cpp

#include <iostream>
#include <windows.h>
#include <iomanip>
#include <string>
#include <cstring>
#include <algorithm>
#include <unistd.h>
#include <stdio.h>

using namespace std;

void AppUI::SearchBook()
{
    system("CLS");
    TitleHeader();
    setTxtColor(10);
    PageTitle("Search Book");

    cout << "Search books by:" << endl;
    cout << "1. Title" << endl;
    cout << "2. Author" << endl;
    cout << "3. Publication Date" << endl;
    cout << "4. Publisher" << endl;
    cout << "\n0. Go back to main menu" << endl;
}

EditBook.cpp

#include <iostream>
#include <windows.h>
#include <iomanip>
#include <string>
#include <cstring>
#include <algorithm>
#include <unistd.h>
#include <stdio.h>

using namespace std;

void EditBook::SearchBook()
{
    //variable declarations
    int Opt;
    char searchTxt[255];

    SearchStart:
    UI.SearchBook();
    cout << "\nOption: ";
    cin >> Opt;

    switch(Opt)
    {
        case 0:
            UI.MainMenu();
            break;

        case 1:
            system("CLS");
            cout << "Enter title: ";
            cin.getline(searchTxt,sizeof(searchTxt));
            SearchByTitle(searchTxt);
            break;

        case 2:
            system("CLS");
            cout << "Enter author name: ";
            cin.getline(searchTxt,sizeof(searchTxt));
            SearchByAuthor(searchTxt);
            break;

        case 3:
            system("CLS");
            cout << "Enter publication date: ";
            cin.getline(searchTxt,sizeof(searchTxt));
            SearchByPubDate(searchTxt);
            break;

        case 4:
            system("CLS");
            cout << "Enter publisher: ";
            cin.getline(searchTxt,sizeof(searchTxt));
            SearchByPublisher(searchTxt);
            break;

        default:
            cout << "Invalid option!";
            sleep(1);
            goto SearchStart;
            break;
    }
}

In the Search book, when I input a digit not available in the options like "5", it loops back, and lets me enter the correct option. But when I input a letter, like "a" for exmple, it loops back infitely making it display "invalid option" over and over and not letting me input a new option. I was hoping that when I input a letter, which is an invalid option, it would still go back and let me input the correct one, which is a number/integer.

  • I would declare `char Opt[256];` so that I can accept all text input, but only proceed on a valid number else, I keep prompting for input in a while loop. https://gist.github.com/stephenquan/3f0496294627e907eca5e520e9030d7b – Stephen Quan Nov 28 '22 at 07:34

2 Answers2

1

Well following the existing code you need

if (!(cin >> Opt))
{
    cin.clear();            // clear stream error state
    cin.ignore(100, '\n');  // ignore any pending input
    goto SearchStart;       // loop back
}

Of course I cannot recommend using a goto, write a proper loop.

I don't want to knock my own answer but the best solution is Yunnosch's, but maybe that is a little more difficult to code for a beginner.

john
  • 85,011
  • 4
  • 57
  • 81
0

You want to "loop back when user inputs letter".
So you want to allow the user to input letters, so as to detect that and react to it. For that you need to accept more than int as input; which you do not with int Opt; and cin >> Opt;.

You need something which can be both, integer or character.
E.g. a string. Then interpret it and only use as integer when it can be used as integer.

Yunnosch
  • 26,130
  • 9
  • 42
  • 54