Switch statement auto-default

the statement automatically goes to default no matter what I put in as the cin
help?

#include <iostream>
using namespace std;

int main ()
{
char answer, no, No, yes, Yes;
cout << "Tsuki! Did you know? (yes, or no?)\n";
cin >> answer;

switch (answer)
{
case 1:
answer == no;
cout << "You didnt? Well then I guess you should know. I love you!\n";
break;

case 2:
answer == yes;
cout << "You did? You must be some sort of amazing! Well played!\n";
break;

default:
cout << "stop playing games, my love!\n";
break;
}

}
You declare
char answer;

so your switch should compare to char

e.g
switch (answer)
{
case '1':
answer == no;
cout << "You didnt? Well then I guess you should know. I love you!\n";
break;

case '2':
answer == yes;
cout << "You did? You must be some sort of amazing! Well played!\n";
break;

default:
cout << "stop playing games, my love!\n";
break;
}
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
#include <iostream>
using namespace std;

int main ()
{
char answer;
cout << "Tsuki! Did you know?  'y' Yes, 'n' No?\n";
cin >> answer;

switch (answer)
{
case 'n':
cout << "You didnt? Well then I guess you should know. I love you!\n";
break;

case 'y':
cout << "You did? You must be some sort of amazing! Well played!\n";
break;

default:
cout << "stop playing games, my love!\n";
break;
}

cin.ignore().get();
return 0;
}
What do you think the value of answer is?
Go back and check how a switch statement works.

Your switch statement is expecting answer to have the binary value of 1 or 2.
The statements
answer == no;
answer == yes;

will have no effect. The condition will be evaluated if that branch of the switch statement is executed, but the result is ignored.
Topic archived. No new replies allowed.