0

I have written a program that simulates a fishing game. The program is inside a running loop to iterate as long as the user wishes. The issue is that anything that is entered, other than 1 or 0, breaks my code. I have been trying different things for hours now, and I need some help! My main cpp file code is included. Please let me know if my header files are needed for your review as well.

It has been requested for me to edit the code. The problem is that anything other than an integer breaks my code when entered. I do not want this. I want the program to catch the input and continue looping until the user enter's the right input ( 1 or 0) or quits the game.

Game game; // Game object game declared
cout<<"Welcome to Go Fish 2.0!"<<endl; // welcome message
cout<<"Would you like to play?"<<endl; // Prompts user to play
cout<<"1 to play, 0 to exit:"<<endl; // Provides user with input choices
cin>>choice; // user enters game play choice

if (choice == 1) // while user chooses to play game
play = true; // play boolean is set to true
else if (choice == 0) // while user chooses to end game
   play = false; // play boolean is false
while ( ! cin>>choice) { // while user entry is not valid
// display correct inpout choices again
    cin.clear();
    cin.ignore (100, '\n');
cout <<"You entered invalid data. Please enter the numerical value 1 if you want to play again or 0 if you dont."<<endl;
cin >> choice; // hopefully user enters valid input

if (choice == 1) { // if user chooses proper input after improper input
play = true;// play boolean is set to true
break;// break
}
}


total=0; // variable total is initialized to 0
while (play == true) { // while play boolean is set to true; user wants to play game

total1 += game.playgame(total); // total1 variable keeps a running total of the game
//game.playgame(total) uses the game object to call the playgame function that passes the variable total
// when game.playgame(total) is called this funciton essentially mobilizes game play of die rolling and point accumulation
//each time this function is called (as many times as user wants to play game), the running total is incremented

cout<< "Do you want to play again? (0 for no, 1 for yes)"<<endl;// asks user if they want to play again
cin >> choice;// user enters choice

if (choice == 1) // if user enters 1
{
play = true; // play is assigned to true
}


else if (choice == 0) // if user enters 0
{
play = false; // play is assigned to false
}
/*  
while ( ! cin>>choice) { // while user entry is not valid
// display correct inpout choices again
    cin.clear();
    cin.ignore (100, '\n');
cout <<"You entered invalid data. Please enter the numerical value 1 if you want to play again or 0 if you dont."<<endl;
cin >> choice; // hopefully user enters valid input

if (choice == 1) { // if user chooses proper input after improper input
play = true;// play boolean is set to true
break;// break
}
}


if (choice == 1) { // if user chooses proper input after improper input
play = true;// play boolean is set to true
break;// break
cout<<"My Total game points are "<<total1<<endl; // displays user's total points from whole game
if (total1>100) // if game total greater than 100
cout<<"Awesome! You're a great fisher!"<<endl; // congratulate user
if (total1<100)// if game total is less than 100
cout<<"Good job, but play again so you can fish some more."<<endl;// chastise user
system("pause");
}

}
return 0;// end main function
}
}
6
  • Please post a Minimal, Complete, and Verifiable example Commented Dec 2, 2015 at 23:55
  • 2
    please for the love jove - format your code... Commented Dec 3, 2015 at 0:01
  • Instead of rand, take a look at std::uniform_int_distribution. It will give you better randomness and front-loads all of the difficulty in use. (Or in English, harder to set up, but then it is fire and forget) Commented Dec 3, 2015 at 0:23
  • Last edit made things worse. Stop messing with this code and write a simple little program that does nothing but read a 1 or 0 from the user. Play with that for a while. If you can't get it to work, you are in great position to ask a tightly focused, complete question. Commented Dec 3, 2015 at 0:36
  • Please use something like this for formatting the code :- prettyprinter.de . It is difficult for a lot of people to follow the code. You will get better responses if you indent your code properly. Commented Dec 3, 2015 at 1:21

2 Answers 2

1

You should take input from the user as a string/character array first and then attempt to convert it to an int (there are a number of ways to do this - Google is your friend ;)) but atoi (ASCII to int) is probably the quickest/simplest. If that fails, ask the user to enter a valid input, repeat until you get a 1 or 0, something like that.

Sign up to request clarification or add additional context in comments.

1 Comment

atoi is no better than cin directly to an int. This is the right idea, but you want strtol or std::stoi to ensure the user typed in 1 or 0 and nothing else.
0

There are multiple places where you expect input. I couldn't make much sense of your code.

Based on your description you just need one place to read the integer: a loop reading the choice and verifying the correct input was received. That should be rather straight forward...

 for (int choice(0); true; ) {
     std::cout << "some message goes here\n";
     if (std::cin >> choice) {   // successfully read a value
         if (choice == 0) {      // the choice is to stop
             break;
         }
         else if (choice == 1) { // the choice is to carry on
             play_again();
         }
         else {                  // an invalid choice was made
             std::cout << "value " << choice << " is out of range\n"
         }
     }
     else if (std::cin.eof()) {  // input failed because nor more input
         break;
     }
     else {                      // a non-integer was entered
         std::cout << "a non-integer was entered\n";
         std::cin.clear(); // clear error state and ignore current line
         std::cin.ignore(std::numeric_limits<std::streamsize>::max();
     }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.