1

I have a do while loop that asks the user to enter numbers 1 - 4, and if they enter a wrong number, a letter, or a special character, it loops back to asking them to choose between the numbers 1 - 4 at the main menu. It works when I enter a wrong integer value and loops back to the initial 4 options, but when I enter a letter or special character, the main menu is constantly output to the screen. I'm suspecting it may have something to do with the user input being of int data type, but I'm just too new to c++. Here is my code, I attached one of the functions the main menu calls to give a better perspective:

#include "dec.hpp"
#include<iostream>
#include<string>
#include<vector>

 void options() {
    int userNum;
    char yesNo;
    do {
    std::cout << "What would you like to do? Please enter the number for each option, then press enter." << std::endl;
    std::cout << "Make sure to enter a valid number! (1 - 4)" << std::endl;
    std::cout << "---------------------" << std::endl;
    std::cout << "1. Register a customer for the reward system." << std::endl;
    std::cout << "2. Remove a current member of the reward system." << std::endl;
    std::cout << "3. Access the list of existing reward system members." << std::endl;
    std::cout << "4. Access to the current list of items in the reward system." << std::endl;
    std::cin >> userNum;
    std::cin.sync();
    if (userNum >= 1 && userNum <= 4)
        break;
    else {
        yesNo = 'N';
        std::cout << "Please make sure to only enter the numbers available in the menu, other numbers and letters/special characters will not be accepted." << std::endl;
    }
    } while (userNum < 1 || userNum > 4 || yesNo == 'N' );
    
    if(userNum == 1) 
        registerCustomer();
    if (userNum == 2) 
        removeCustomer();
    if (userNum == 3) 
        inspectCustomers();
    if (userNum == 4) 
        inspectItems();
}

void registerCustomer() {
    std::vector<std::string> nameList;
    std::string newName;
    char yesNo;
    int answer;
    do { 
    std::cout << "Would you like to register a new member? If not, input 0 to look at other options!" << std::endl;
    std::cout<< "Otherwise, input 1 to continue to registration process." << std::endl;
    std::cin >> answer;
    if (answer == 0) 
        options();
    
    if (answer==1) {
        do {
        std::cout << "Please input preferred username of the new member." << std::endl;
        std::cin >> newName;
        std::cout << "Is " << newName << " the name you would like to register? If so, enter 'Y'." << std::endl;
        std::cout << "Otherwise, enter 'N' to register another name." << std::endl;
        std::cout << "Enter any other key to go back to the main menu!" << std::endl;
        std::cin >> yesNo;
        if (yesNo == 'Y' || yesNo == 'y')
        //enter 'Y' or 'y' takes user back to options() menu, fix later on// 
            nameList.push_back(newName);
        if (yesNo == 'N' || yesNo == 'n') 
            yesNo = 'N';
        else
            options();
    } while (yesNo == 'N');
    }

    else {
        std::cout << "Sorry, that's an invalid input. Please input a valid option." << std::endl;
        answer = 'N';
    }
    } while (answer == 'N');
wawa
  • 127
  • 5
  • 2
    Before using the result of `std::cin >> userNum;`, you should verify that the conversion was successful. For example, you should print an error message when the condition `if ( std::cin.fail() )` is true. – Andreas Wenzel Nov 12 '21 at 02:11
  • 1
    *"I'm suspecting it may have something to do with the user input being of int data type,"* -- this is something you could test in less time than it took you to write your question. Experimentation is good! Change the type of `userNum` to `char` and change the `1` and `4` in your `if` statement to `'1'` and `'4'`. Did this make any difference? – JaMiT Nov 12 '21 at 03:27
  • 1
    __Debugging tip:__ Right before your loop ends (between the two closing braces), add a line like `std::cout << "while (" << userNum << " < 1 || " << userNum << " > 4 || '" << yesNo << "' == 'N' )\n";` so that you can see what your loop condition looks like with current values. – JaMiT Nov 12 '21 at 03:29

0 Answers0