How do I show my switch function that my string variable equals user input

55 views Asked by At

I have a program with a string variable that equals whatever the user puts in. I am trying to use a switch statement to display a certain message based on the letter the user typed, but don't know how to show the switch program it is an input. I am grateful for any help, part of the code is below.

cin >> ans1;
switch(ans1 == "") {
    {case "A" : cout << ": Sorry, the right answer was C.";
    break;}
    {case "B" : cout << ": Sorry, the right answer was C.";
    break;}
    {case "C" : cout << ": Correct! Wow, your pretty smart!";
    break;}
    {case "D" : cout << ": Sorry, the correct answer was C.";
    break;}
1

There are 1 answers

0
Ryan Shesler On BEST ANSWER

Instead of using a switch statement with a variable that equals user input, you could use an if/else statement (i.e. ans1 == 'C').

if (ans1 == 'C') {
    cout << "Correct";
} else {
    cout << "Wrong";
}

Thank you JohnnyMopp5.