Need Help With IF Statements

I need some help with my program. I was writing a text-based game and used an if statement in my game, but wrote this other "simple" program to see if someone could help me understand it and to make it function correctly. I tried many different ways and I can't get it to work.

I want the user to enter bat or ball. If the user enters bat, the program says you picked the bat. If the user enters ball, the program say you picked the ball. And the issue im having problems solving is when the user doesn't enter the correct response of bat or ball, but with other choices like house or sand castle. If a user inputs a choice other than bat or ball, I want the program to say, You didn't pick the bat or ball. Then tell the user to enter the choice bat or ball and once they do it will say, you picked the bat or you picked the ball.

I would greatly appreciate any help. Thanks.



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
#include <iostream>
#include <string>
#include <math.h>
#include <cstdlib>


using namespace std;

int main ()

{
    string choice;
    cout << "Would you like to pick the bat or ball? Enter choice: ";
    cin >> choice;
    
    
    if (choice == "bat")
    {
        cout << "You picked the bat.";
    }
    
    if (choice == "ball")
    {
        cout << "You picked the ball.";
    }
    
    if (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;
     }
    
    
http://www.cplusplus.com/forum/general/134326/#msg718306

1
2
3
4
5
6
7
8
if( choice=="bat" )
   //...
else if( choice=="ball" )
   //...
else{  
   cout << " You didn't pick the bat or ball." << endl;
   //...
}
So the code i have now is but when i enter the something other than bat or ball it says unrecognized choice (as it is suppose to) however when i enter the right choice as bat or ball how do i get the output the say "you pick the bat" or "you picked the ball"?


#include <iostream>
#include <string>
#include <math.h>
#include <cstdlib>


using namespace std;

int main ()

{
string choice;
cout << "Would you like to pick the bat or ball? Enter choice: ";
cin >> choice;


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

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


return 0;
}



I think i got it working correctly with this code:




#include <iostream>
#include <string>
#include <math.h>
#include <cstdlib>


using namespace std;

int main ()

{
string choice;
cout << "Would you like to pick the bat or ball? Enter choice: ";
cin >> choice;


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

}
else
{
cout << "You didn't pick the bat or ball." << endl;
cout << "Unrecognized choice: " << choice << "." << endl << "Please enter the choices bat or ball. Enter choice: ";
cin >> choice;
cin.clear();
cout << endl;


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

cout << endl; return 7;


}

You could use a while loop to validate that the entry is valid instead of repeating code.

e.g.

while entry is not bat or ball
--- display error message
--- prompt reentry

then your if statements to output what they picked.
You do now:
1. Ask
2. Test
3. If wrong, ask again
4. ?


What you do need is a loop. Ask repeatedly, until the answer is correct.
1
2
3
4
5
6
7
8
string answer = "foo";
cout << "Do you feel lucky, punk?";
while ( cin >> answer && "no" != answer && "yes" != answer ) {
  cout << "guess again";
}
if ( "no" == answer  || "yes" == answer ) {
  // use answer
}
no i don't know how to use loops yet, i just started coding like 5 days ago, teaching myself.
Topic archived. No new replies allowed.