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.
#include <iostream>
#include <string>
#include <math.h>
#include <cstdlib>
usingnamespace 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;
}
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"?
{
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;
}