0

A user has to select a choice from the menu, and the program's goal is to check whether the user has selected a valid choice or not. In python I would run a while loop and compare them using "in": (userChoice in validChoices). How do I do that in C++ using a while loop?

Valid choices are stored in this variable:

const char validChoices[12] = {'P', 'p', 'A', 'a', 'M', 'm', 'S', 's', 'L', 'l', 'Q', 'q'}

The program asks the user for input and stores it in a char variable:

char userChoice {};
std::cin >> userChoice;

If the choice is invalid, the program should say "Unknown selection, please try again" and ask again for the input until the user selects a valid option.

Appreciate the help in advance!! Been struggling with it a lot.

Current attempt:

bool isValidChoice = true;


while (isValidChoice)
{
    char userChoice {};
    std::cout << "Select a choice: ";
    std::cin >> userChoice;

    if ()
    {
        std::cout << "You have selected: " << userChoice << std::endl;
        isValidChoice = false;
    }
    else
    {
        std::cout << "Unknown selection, please try again" << std::endl;
    }
  • You can use a loop or a standard algorithm. Both will work relatively well. – Martin York Oct 16 '22 at 21:25
  • *How do I do that in C++ using a while loop?* -- This can be done without a `while` loop. – PaulMcKenzie Oct 16 '22 at 21:26
  • [Edit] your question to show your attempt so far. – Stephen Newell Oct 16 '22 at 21:27
  • Thank you for looking at my question. I checked this website https://www.techiedelight.com/check-if-an-element-exists-in-an-array-cpp/ but it seems kind of advanced . I am wondering if there is a more basic way to do that. – ceolorenso Oct 16 '22 at 21:33
  • *A user has to select a choice from the menu* -- What you should really be doing is write a very small `main` program to familiarize yourself with how to search for an item in a container. Then when you see how it's done in the smaller program, you use it in the larger program. – PaulMcKenzie Oct 16 '22 at 21:34
  • Also, you really should be using good C++ books to learn from, not websites. – PaulMcKenzie Oct 16 '22 at 21:36
  • I use Udemy for learning. Do you know any good books for learning C++? – ceolorenso Oct 16 '22 at 21:41
  • There are some here [The Definitive C++ Book Guide and List](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – Ted Lyngmo Oct 16 '22 at 21:41
  • Thank you, Ted! I will definitely take a look at those books. – ceolorenso Oct 16 '22 at 21:47

2 Answers2

1

You can simply call

auto it = std::find(std::begin(validChoices), std::end(validChoices), userChoice);

by checking statement

if(it != validChoices.end())

it means that your choice have been found in the validChoices, because userChoice value have been found before validChoices end structure iterator. If it would be validChoices.end() that would mean that it just couldn't be found.

MonteChrist0
  • 151
  • 4
0

There are many ways to solve this issue without having to write a while loop.

One way is to store the valid input in a string, and search the string using strchr:

#include <cstring>
#include <iostream>

bool isValidChoice(char ch)
{
   const char *validChoices = "PpAaMmSsLlQq";
   return strchr(validChoices, ch);
}
 
int main()
{
   std::cout << isValidChoice('P') << "\n";   
   std::cout << isValidChoice('s') << "\n";   
   std::cout << isValidChoice('Y') << "\n";   
}

Output:

1
1
0
PaulMcKenzie
  • 34,698
  • 4
  • 24
  • 45