Multiple Choice user input not working

Hey there I am trying to get this program to have a choice of three options A, B, or C and I can't seem to lock it down to just "A or a = option A" or " B or b = option B". With what I have it just seems to allow any character being pressed to keep the program moving forward. So how do I get it so when the user presses a or A the go to option A and so on. Also as a not it has to be A, B and C not 1, 2 and 3.

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
33
34
  #include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{
	char choice;
		cout << "a: Display an example\n";
		cout << "b: Try it out yourself\n";
		cout << "c: Exit\n";
		cout << "\n";
		cout << "press enter, make your choice, press enter";
		cin.ignore();
		cin >> choice;
		cin.ignore(numeric_limits<streamsize>::max(), '\n'); // http://www.cplusplus.com/forum/beginner/121194/
	
	if(choice == "A" && "a");
	{
		cout << "First number: 901\n";
		cout << "The number reversed: 109\n";
		cout << "Subtract the reversal from the original number: 901-109=792\n";
		cout << "Finally, reverse the resulting number: 792\n";
		cout << "Add it to its un-reversed form: 297+792 and\n";
		cout << "the program will output the final result (1089 if you did it right)\n";
	}
	
	if ("B" && "b");
	{
		cout << "cool\n";
	}
	system("PAUSE");
	return 0;

	
this: if(choice == "A" && "a"); should look like this - if(choice == 'A' || choice == 'a') // remove the semicolon, no semicolons after if statements

note the ' ' instead of " ". Becasue they're chars, not strings. Also note ||instead of &&. A character can't both be uppercase and lowercase at the same time, you want either or. There should also be choice == on the other side of the operators, not just on the left.

if ("B" && "b"); should be: if (choice == 'B' || choice == 'b') // remove semicolon

Edit:
The ignore before cin >> choice; serves no purpose, you can remove it.
Use if else instead of a second if statement.

1
2
if(something) {}
else if(something else) {}


Full program:

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
#include <iostream>
using namespace std;

int main()
{
	char choice;
	cout << "a: Display an example\n";
	cout << "b: Try it out yourself\n";
	cout << "c: Exit\n";
	cout << "\n";
	cout << "press enter, make your choice, press enter";
	cin >> choice;
	cin.ignore(numeric_limits<streamsize>::max(), '\n'); // http://www.cplusplus.com/forum/beginner/121194/

	if (choice == 'A' || choice == 'a')
	{
		cout << "First number: 901\n";
		cout << "The number reversed: 109\n";
		cout << "Subtract the reversal from the original number: 901-109=792\n";
		cout << "Finally, reverse the resulting number: 792\n";
		cout << "Add it to its un-reversed form: 297+792 and\n";
		cout << "the program will output the final result (1089 if you did it right)\n";
	}

	else if (choice == 'B' || choice == 'b')
	{
		cout << "cool\n";
	}
	system("PAUSE");
	return 0;
}
Last edited on
Well right at first glance, you're saying that choice has to equal 'A' AND 'a'. If you want to use OR you need ||, I believe. Also, at the bottom if statement you need to say if(choice == 'B' || choice == 'b'). And you don't need semi colons at the ends of your if statements. There is also a missing brace near the bottom.
Last edited on
thanks to the both of you, obviously Im super new at this
Also, at the bottom if statement you need to say if(choice == 'B' || 'b')

No, should be if(choice == 'B' || choice == 'b')

thanks to the both of you, obviously Im super new at this

You're doing a decent job so far. Keep it going, goodluck =)
Last edited on
Topic archived. No new replies allowed.