First questions on this forum, and i am very new at C++ and programming so forgive me if i seem a bit dumb.
I need help with an assignment. Basically i need to know how to create a "cin" statement that will prompt a user to for multiple options.
So the user would be asked for either A, B, C or D, and depending if the user chose one of those statements, the program would perform a task. Here is what i have so far.
So the program is designed to ask the user if they want to calculate one of the 4 dimensions listed below.
I don't understand how I am supposed to ask a user for multiple options.
An integer is easy. I can just declare a value to be an int and ask the user to input that int. But how do I do that for a character?
Say i want the user to enter a character and depending on what that user enters, that character will ask them to enter more information or compute a value.
how do declare a list of characters for the user to choose from?
I guess i am just confused about how to enter this stuff as code.
char input;
std::cin >> input;
input = std::toupper(input); // Convert to uppercase (needs #include <cctype>)
while (input != 'R' && input != 'C' && input != 'P' && input != 'S')
{
std::cout << "That's not one of the options, try again: ";
std::cin >> input;
}
// Now 'input' is either R, C, P, or S.
// Use if or switch statements to deal with each of those cases
Alternatively, if you think you're going to add more options later and you want this to scale nicely, you could also do something like
1 2 3 4 5 6 7 8 9 10 11
char input;
std::cin >> input;
input = std::toupper(input);
const std::string Options = "RCPS";
while (Options.find(input) == std::string::npos)
{
std::cout << "That's not one of the options, try again: ";
std::cin >> input;
}
// Now 'input' is one of the characters in the 'Options' string.