-4

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;}
Ryan Shesler
  • 119
  • 8
  • 1
    `switch(ans1 == "")` ?? That can only have values true or false, not "A", "B" etc... – John3136 Jan 04 '17 at 01:50
  • 3
    See here for all the answers to your question: http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – Sam Varshavchik Jan 04 '17 at 01:51
  • 2
    Also, you can't use a `string` with a `switch` statement. But you can use a `char`. Why not just use `if/else`? `if (ans1 == "C") {cout << "Correct;} else {cout << "Wrong";}` – 001 Jan 04 '17 at 01:52
  • how would I say that ans1 would equal user input, and if the input was a, b, c, d – Ryan Shesler Jan 04 '17 at 01:57
  • oh ok thank you that makes more sense @JohnnyMopp – Ryan Shesler Jan 04 '17 at 01:58
  • @JohnnyMopp sorry to bother you but I started using that program and when i ran it the output would be cout << "right"; whether I got it wrong or right. i put some of the code with the if else statements below – Ryan Shesler Jan 04 '17 at 03:06
  • ` cout << "\n" << first << "\n \n \n \n" << "Messages carried in the nerves in your brain travel how fast?" << "\n \n A- 1,000 mph \n \n B- 200 mph \n \n C- 400 mph \n \n D- 100 mph"; cout << "\n \n \n \n \n \n \n \n \n \n \n"; cin >> ans2; if(ans2 == 'B', 'B ', 'b', 'b ') { cout << "Wow! Right again!"; } else { cout << "Aww, bummer. The right answer was B."; } ` – Ryan Shesler Jan 04 '17 at 03:08
  • What do you mean "show the switch program it is an input?" Also, @John3136 is absolutely correct here. What do you want that line to do (i.e. what did you *think* that that would do)? One final thing: have you stepped through this with a debugger? (Please see [this article](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) for advice on that). – EJoshuaS - Stand with Ukraine Jan 04 '17 at 06:54
  • `if(ans2 == 'B', 'B ', 'b', 'b ')` That is not how you compare multiple values. Instead do this: `if (ans2 == 'B' || ans2 == 'b')`. Or you could just convert to upper case: `if (std::toupper(ans2) == 'B')`. Also, I am assuming `ans2` is a `char` and not a `std::string`. – 001 Jan 04 '17 at 13:08
  • I never seen such a switch before – Raindrop7 Jan 07 '17 at 23:01

1 Answers1

0

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.

Ryan Shesler
  • 119
  • 8