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.
#include "stdafx.h"
#include <iostream>
usingnamespace 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.
#include <iostream>
usingnamespace 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";
}
elseif (choice == 'B' || choice == 'b')
{
cout << "cool\n";
}
system("PAUSE");
return 0;
}
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.