0
void menu_prompt(vector<int> list){
char user_input{};
do {
    cout << "P - Print numbers" << endl;
    cout << "A - Add a number" << endl;
    cout << "M - Display mean of the numbers" << endl;
    cout << "S - Display the smallest number" << endl;
    cout << "L - Display the largest number" << endl;
    cout << "Q - Quit" << endl << endl;
    cout << "Enter your choice: " << endl;

    cin >> user_input;
} while(user_input != 'P' || 'A' || 'M' || 'S' || 'L' || 'Q' || 'p' || 'a' || 'm' || 's' || 'l' || 'q');
if(user_input == 'P' || 'p'){
    user_choice_print_numbers(list);
}
else if(user_input == 'A' || 'a'){
    user_choice_add_numbers(list);
}
else if(user_input == 'M' || 'm'){
    user_choice_mean_numbers(list);
}
else if(user_input == 'S' || 's'){
    user_choice_smallest_numbers(list);
}
else if(user_input == 'L' || 'l'){
    user_choice_largest_number(list);
}
else if(user_input == 'Q' || 'q'){
    user_choice_quit();
}

}

I have a menu function that I chose to do a do-while loop in order to prompt the menu at least once. Whenever I input the following characters, the menu is prompted again. I have a gut feeling that this issue either has to do with scoping or not manipulating the variable user_input correctly since the do-while loop's escape is based on user_input.

My logic is the "do" part executes the print statements and will prompt for the user to change user_input. If none of the characters shown in the escape statement are inputted, the menu will be prompted again.

I haven't posted the entire program because it's somewhat lengthy and I don't think other parts are affecting this issue. But the gist is certain inputs will call certain functions already created previously.

SK Sam
  • 37
  • 7
  • 1
    `user_input != 'P' || 'A' || ` isn't the correct syntax and will always evaluate as true. You'll need `user_input != 'P' && user_input != 'A' && user_input != ...`. – nanofarad Mar 10 '20 at 20:48
  • 1
    `while(user_input != 'P' || 'A')` same as `while((user_input != 'P') || 'A')` and always returns true. – Tarek Dakhran Mar 10 '20 at 20:48
  • 1
    Use a vector to check several menu options. This question is not an exact duplicate of the other one: ` static vector menuOptions{'P', 'A', 'M', 'S', 'L', 'Q', 'p', 'a', 'm', 's', 'l', 'q'}; char user_input{}; do { cout << "P - Print numbers" << endl; //... etc. cin >> user_input; } while (std::find(menuOptions.begin(), menuOptions.end(), user_input) == menuOptions.end()); ` – Alex Mar 10 '20 at 20:52
  • @Alex of course it is not an **exact** duplicate, but it is a good enough fit. You could post your answer on the duplicate. Code in comments is very hard to read – 463035818_is_not_an_ai Mar 10 '20 at 20:55
  • @Alex Thanks for the input, I'll take that approach next. I'm working with bettering my problem solving and will optimize later. For now I have a misunderstanding of some concept I really want to figure out! – SK Sam Mar 10 '20 at 21:08
  • @ReinstateMonica-ζ-- Thanks Monica! I've corrected my syntax. I'm still hitting an infinite loop and think it has to do with user_input. No matter what I input from the cin >> user_input statement, the compiler still warns me that the while statement will always hold true even with corrected syntax. Updated while statement: while(user_input != 'P' || user_input != 'A' || user_input != 'M' || user_input != 'S' || user_input != 'L' ||user_input != 'Q' || user_input != 'p' || user_input != 'a' || user_input != 'm' || user_input != 's' || user_input != 'l' || user_input != 'q'); – SK Sam Mar 10 '20 at 21:12
  • 1
    @SKSam I used `&&` and not `||` in my correction. If you use `||` the condition will always be true still. – nanofarad Mar 10 '20 at 21:41
  • 1
    @ReinstateMonica-ζ-- Oh that totally fixed it! Thank you so much. To be honest, I am not 100% sure why I use & vs ||. My totally understand why && works, user_input should be NONE of those values at any time. But my natural response is to use || to tell the compiler "If user_input is neither P or A or M or S or L or Q". It must be rusty boolean logic of mine, I'll try to integrate the use of && to my knowledge. Anyway thank you so much! – SK Sam Mar 10 '20 at 22:10
  • 1
    A brief explanation here: `||` means "input isn't P *or* it's not A... No matter what the input is, it has to either be something other than P, or something other than A (or both). – nanofarad Mar 11 '20 at 03:54

0 Answers0