I need help understanding what is wrong with my program. I have read about if-statements on this website but that hasn't helped me much. When a user inputs bat, i want the program to say "You picked the bat." When a user inputs ball, i want the program to say "You picked the ball." If a user doesn''t input either bat or ball i want the program to say "You didn't pick the bat or ball. Unrecognized choice: (whatever the user inputed). Please enter the choices bat or ball. Enter choice: ". If the user continues to input anything other than bat or ball the same error prompt comes up "You didn't pick the bat or ball.... Please enter the choices bat or ball..." When the user finally enters bat when the error message comes up i want the program to say "You picked the bat." when the user enters ball i want the program to say "You picked the ball."
I just can't the get the program to work properly and don't understand why it isn't.
The problem is not the if statement, its the while loop.
1 2 3 4 5 6 7 8 9
while (choice != "bat" || "ball")
{
cout << "You didn't pick the bat or ball." << endl;
cout << "Unrecognized choice: " << choice << "." << " Please enter the choices bat or ball. Enter choice: ";
cin >> choice;
cin.clear();
cout << endl;
}
This is an infinite loop. choice != "bat" || "ball" always evaluates to true, so the computer processes it forever and the compiler engine crashes. Get rid of the while loop as it is unnecessary. It should look like this:
1 2 3 4 5 6 7 8 9
else
{
cout << "You didn't pick the bat or ball." << endl;
cout << "Unrecognized choice: " << choice << "." << " Please enter the choices bat or ball. Enter choice: ";
cin >> choice;
cin.clear();
cout << endl;
}
---
Also, else means that the conditions in the above if and else if statements all evaluate to false.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
if(var==1) //if var is 1...
{
//do something.
}
elseif(var==2) //also, if var is 2...
{
//do something else.
}
else //btw, if var is not 1 or 2...
{
//do this.
}
#include <iostream>
#include <string>
int main()
{
std::string choice;
// loop while choice is not bat
// or ball.
do
{
std::cout << "Enter bat or ball: ";
std::cin >> choice;
// if you want to show an error you could use an if.
if (choice != "bat" && choice != "ball")
std::cout << "Wrong answer.. " << std::endl;
} while (choice != "bat" && choice != "ball");
std::cout << "Thanks, you selected " << choice << std::endl;
return 0;
}
I don't understand how while (choice != "bat" || "ball") is an infinity loop.
If the code tell you that you entered the wrong choice of bat or ball, then it tells you to pick bat or ball. But if you get an error code and u type in bat or ball wouldnt that end to loop? because the choice is == to bat or ball?
Because the way i understand the do-while loop is: do {statement} while {condition}.
So the loop will continue to print out "You didn't pick the bat or ball. Unrecognized choice: (input). Please enter the choices bat or ball. Enter choice: .... until the choice is bat or ball.
If the choice is not equal to bat or the choice is not equal to ball ----
let's say the choice is bat - then this condition will be satisfied because one of the or clauses (choice not equal to ball) is true.
let's say the choice is ball - then this condition will be satisfied because the other of the clauses (choice not equal to bat) is true.
let's say the choice is some other value - then this condition will be satisfied because neither of the clauses is true.
There is no case where this will ever be false and exit the loop.
Edit: Check out DeMorgan's laws - they're helpful with these conditions
Softrix gave a good example above. You want to use the while loop to validate that you have a valid entry before you get to the point where you output that they chose bat or ball.