Here is my code so far for my program:

#include<iostream>

using namespace std;

int main()
{ 
    int number;
    int guess;
    char reply;
    
    srand (time(NULL));
    
    number = rand() % 1000 + 1;
    
    do
    {
           do
           {
                    cout << "Guess the Number" << endl;
                    cin >> guess;
                    
                    if (guess > number)
                    {
                              cout << "Too High" << endl;
                    }
                    else if (guess < number)
                    {
                              cout << "Too Low" << endl;
                    }
                    else if (guess == number)
                    {
                              cout << "Correct" << endl;
                    }
                    else
                    {
                              cout << "Invalid Input" << endl;
                    }
           } while (guess != number);
    
           cout << "Would you like to play again? <Y/N>" << endl;
           cin >> reply;
           
    } while (reply == 'y' || reply == 'Y');
}

My problem is that when I input a word or something besides a number for the guess, the program runs the code thousands of times and glitches out. I want to make it like:

if (guess != "a number") then it outputs something and you can continue to guess.

I've not been doing c++ very long and am a beginner and would like some help.

Thank you.

Sometimes data is left in the buffer of cin.

check cin for !good(), cin.good() is not the exact opposite of cin.bad(). good() will return true if no error bits are set, false otherwise. Afterward you will want to clear() the errors bit, and sync() the buffer associated with the stream to its controlled input sequence.

int number = 0;
cin >> number;
if( cin.bad() )
{
  //badbit is set
  //stream is totally f'd up now.
  return 1;
}
else if( !cin.good() )
{
  cin.sync();//sync
  cin.clear();//clear error bits.
  //stream can now be used again.
}

Note that sync() can return a value.

http://www.cplusplus.com/reference/iostream/istream/


cin is an object of type istream.
http://www.cplusplus.com/reference/iostream/cin/

Edited 5 Years Ago by pseudorandom21: n/a

A suggestion is to make a get() function that checks for errors, for example :

template<typename InputType>
InputType getType(){
 InputType var = InputType();
 while( ! (cin >> var) ){
    cout << "\nInput failed...try again : "; //print error message if failed
    cin.clear(); //clear the error bits
    while(cin.get() != '\n') continue; // throw away the junk left in the stream
    //then try again
 }
 return var;
}
int main(){
 int i = getType<int>();
}

Ok so I've got:

if (isalpha(guess))
                    {
                              cout << "Invalid Input" << endl;
                    }

so far. Is there a way that the program could restart or restart from a specific point?

Here's what you want to do IMO.

  1. Ask user for input.
  2. Read in the input as a string.
  3. Test the string for valid input (i.e. all digits).
  4. If needed, check for/clear any bad bits and/or clear the stream so you'll be ready for step 1 again the next time, if needed. Since you're reading into a string, you don't actually need to clear any bad bits. That's the whole point of reading it into the string rather than an int in the first place.
  5. If input is valid, convert it to an int and proceed with what you originally had in lines 22 - 37.
  6. If input is invalid, give an error message and return to step 1.

You shouldn't be "restarting" anything. If you set up the loops/if statements correctly, the right code gets executed and/or skipped and/or repeated.

Thanks for the help, but I have no idea how to read the inputs as a string, check/clear for bad bits or any of that. I started 3 days ago with C++, and I think I'm just going to wait until I know some more before I go asking questions.

This article has been dead for over six months. Start a new discussion instead.