0

I have recently made a menu through a do/while loop to perform a set of actions. The menu appears in the terminal and lets me make a selection without any problems. However, when I finally make my choice, the functions never seem to be called. This results in being returned to the main part of the menu with no action undertaken. I have attached a smaller code snippet below as an example:

void Menu()
{
    std::cout<<"-----------------------------------------------------"<<std::endl;
    std::cout<<"\t \t Welcome. \t \t"<<std::endl;
    std::cout<<"-----------------------------------------------------"<<std::endl;
    std::cout<<"1. Input Data for objects (Person, Dependent, Employeem Manager, Worker)."<<std::endl;
    std::cout<<"2. Inquire values."<<std::endl;
    std::cout<<"3. Replace values."<<std::endl;
    std::cout<<"4. Quit.";
    std::cout<<"Enter your choice and press return."<<std::endl;
}

int main(void)
{
    Person P;
    Dependent D;
    Employee E;
    Manager M;
    Worker W;
    
    Menu();
    int user_input;
    char answer;
    do
    {
        std::cout<<"Select an option"<<std::endl;
        std::cin>>user_input;
        switch (user_input)
        {
            case 1:
            {
                std::string choice;
                std::cout<<"Pick an object type (P/p, D/d, E/e, M/m, or W/w)"<<std::endl;
                std::getline(std::cin, choice);
                if (choice == "P" || choice == "p")
                {
                    P.create_data();
                }
            }
        }
        std::cout<<"Press anything but 4 to continue"<<std::endl;
        std::cin>>answer;
    }
    while (answer != 4);
}

Also, here is the function being called:

void Person::create_data()
{
    std::cout<<"Name: ";
    std::cin>>name;
    std::cout<<"Date of Birth: ";
    std::cin>>date_of_birth;
    std::cout<<"Gender: ";
    std::cin>>gender;
    std::cout<<"SSN: ";
    std::cin>>SSN;
    std::cout<<"Address: ";
    std::cin>>address;
    std::cout<<"Home Phone: ";
    std::cin>>home_phone_number;
}

Thanks.

JCPowell
  • 21
  • 2
  • Please add a default case to your switch. – kiner_shah Jun 29 '22 at 07:17
  • 4
    I suspect that [this fact about `getline` and `>>`](https://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction) is the problem. The lack of details makes me not absolutely sure. – molbdnilo Jun 29 '22 at 07:18
  • @zin, that is not the problem. – kiner_shah Jun 29 '22 at 07:19
  • 1
    Try with `std::cin >> choice;`, does that help? – john Jun 29 '22 at 07:22
  • @kiner_shah I have a default switch. I just did not include in the snippet – JCPowell Jun 29 '22 at 07:28
  • @molbdnilo this was the issue. thank you – JCPowell Jun 29 '22 at 07:30
  • *"I have attached a smaller code snippet below as an example:"* -- smaller is good. You could do better, though. Assuming the content of the menu does not affect this functionality, your menu function could have been as simple as `void Menu() { std::cout<<"This is the menu.\n"; }`. – JaMiT Jun 29 '22 at 07:42
  • *"I have attached a smaller code snippet below as an example:"* -- sometimes adding code is as helpful as removing code. In particular, a few diagnostic outputs would help demonstrate the program flow. After reading a value, output it: `std::cin>>user_input;` `std::cout << "user_input: " << user_input << "\n";` and `std::getline(std::cin, choice);` `std::cout << "choice: " << choice << "\n";`. These are not for your final product, of course. They are there to help readers see what happens without having to run the code (and having to remember what was supposed to be entered as input). – JaMiT Jun 29 '22 at 07:47

0 Answers0