How to prompt user for multiple if statements.

Hello everyone,


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.


#include <iostream>
#include <cmath>
#include <iomanip>
#include <string>

using namespace std;


int main()
{
string R,C,P,S;
cout << "assigment #6 Section #1003" << endl;
cout << "Welcome to the *** Calculator!" << endl;
cout << "Please enter the letter indicated for the figure you want" << endl;
cout << "R - rectangular prism (box)" << endl;
cout << "C - cylinder" << endl;
cout << "P - pyramid" << endl;
cout << "S - sphere" << endl;
cin >> ???????
Last edited on
closed account (18hRX9L8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>

int main(void) {
    char userInput;

    std::cout << "assigment #6 Section #1003" << std::endl;
    std::cout << "Welcome to the *** Calculator!" << std::endl;
    std::cout << "Please enter the letter indicated for the figure you want" << std::endl;
    std::cout << "R - rectangular prism (box)" << std::endl;
    std::cout << "C - cylinder" << std::endl;
    std::cout << "P - pyramid" << std::endl;
    std::cout << "S - sphere" << std::endl;

    std::cin >> userInput;
    std::cin.ignore(); // ignore enter character

    switch(userInput) {
        case 'r':
        case 'R': /* rectangular prism calculation here */ break;
        case 'c':
        case 'C': /* cylinder calculation here */ break;
        case 'p':
        case 'P': /* pyramid calculation here */ break;
        case 's':
        case 'S': /* sphere calculation here */ break;
        default: std::cout << "Wrong input! Try again.";
    }

    std::cout << std::endl << "Press enter when finished... ";
    std::cin.ignore(); // keep console open for user to see results.
    return 0;
}
Last edited on
I am still sort of confused.

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.

1
2
3
4
5
6
7
8
9
10
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. 
Switch is awesome it makes things 10 times easier for character choices.

Thanks for every-ones help :)

Topic archived. No new replies allowed.