NEED HELP WITH IF STATEMENT

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.
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
35
36
37
38
39
40
41
42



#include <iostream>
#include <string>

using namespace std;


   string choice;
        cout << "Would you like to pick the bat or ball? Enter choice: ";
        getline(cin, choice);
        
        
        if (choice == "bat")
        {
            cout << "You picked the bat.";
        }
        else if (choice == "ball")
        {
            cout << "You picked the ball.";
        }
        else
        {
            
            
            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;
            
            }


}

return 0;

}
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.
}

else if(var==2)  //also, if var is 2...
{
//do something else.
}

else //btw, if var is not 1 or 2...
{
//do this.
}
Last edited on

The flow of your code is wrong, take a look at the following. Plus you are using cin again when you could just use one in a do..while loop

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>
#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;
}
Last edited on
so would this be the correct coding?

#include <iostream>
#include <string>

using namespace std;


string choice;
cout << "Would you like to pick the bat or ball? Enter choice: ";
getline(cin, choice);


if (choice == "bat")
{
cout << "You picked the bat.";
}
else if (choice == "ball")
{
cout << "You picked the ball.";
}
else
{

do
{

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;
}
while (choice != "bat" && choice != "ball");

if (choice == "bat")
{
cout << "You picked the bat.";
}
else if (choice == "ball")
{
cout << "You picked the ball.";
}
}

return 7;

}

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.
Last edited on
choice != bat || choice != 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.
Last edited on
I don't understand how while (choice != "bat" || "ball") is an infinity loop.


because it can be rewritten as
 
    while ( (choice != "bat") || ("ball") )
in turn that can be rewritten as
 
    while ( (choice != "bat") || (true) )
which simplifies to
 
     while ( true )


why? Because any non-zero value is considered to be true,
and anything or true results in true.
Thanks for the help :)
Topic archived. No new replies allowed.