Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to allow multiple inputs in case of an incorrect data type entry in cpp?

Tags:

c++

I have a program which generates random number and asks user to keep guessing it until he/she gets it right. I want it to keep accepting new values even if i incorrectly enter any other data type by handling the error cases.

My problem is that when i am trying to run the below program, as soon i input a character and hit enter, it goes into an infinite loop. I tried using cin.ignore() and cin.clear() but that just makes the program stop after the first entry.

Can anyone please help me understand what is going on and how to achieve the desired output? Thanks in advance.

#include <iostream>
#include <cstdlib>
#include <time.h>
using namespace std;

int main()
{
  int secret_num, guess;
  srand(time(NULL));
  secret_num=rand() %  101 + 0;
  cout<<"Enter your guess between 0 and 100: ";

do
 {
  if(!(cin>>guess))
  {
    cout<<" The entered value is not an integer"<<endl;
  }
  else if( isnumber(guess))
    {
      if(guess>secret_num)
        cout<<"Too high";
      else if(guess<secret_num)
        cout<<"too low";
    cout<<endl;
    }
 }
  while(secret_num!=guess);


  if((guess==secret_num)| (isnumber(guess)))
  {
    cout<<"yes the correct number is "<<secret_num<<endl;
  }

  return 0;
}

Edit: Here is a screenshot of what the output looks like with cin.clear() and cin.ignore(1000,'\n') in my code, when i enter a number after entering character twice. enter image description here

like image 916
Curt cobain Avatar asked Dec 28 '25 16:12

Curt cobain


2 Answers

    if (!(cin >> guess))
    {           
        cout << " The entered value is not an integer" << endl;
        cin.clear(); // clear must go before ignore

        // Otherwise ignore will fail (because the stream is still in a bad state)
        cin.ignore(std::numeric_limits<int>::max(), '\n'); 
    }

By default cin.ignore will ignore a single character. If they type more than 1 char, it won't be enough, that's why I've modified it a bit.

if ((guess == secret_num) | (isnumber(guess)))

| is a bitwise operator [OR]

|| is the logical operator [OR]

But I think what you actually want is && (AND)

if ((guess == secret_num) && (isnumber(guess)))
like image 170
Jts Avatar answered Dec 30 '25 06:12

Jts


There're several problems.

  1. You should use cin.clear() and cin.ignore() as @José suggested.

  2. What's isnumber()? I guess it's returning false so no hint message (i.e. "Too high" and "too low") is printed out, looks like it stops although it's just waiting the next input. And isnumber() doesn't make sense to me. guess has been declared as an int, it has to be a number, doesn't it?

  3. if((guess==secret_num)| (isnumber(guess))) is unnecessary here. The loop won't end until the user input the correct number, this condition should have been statisfied.

like image 27
songyuanyao Avatar answered Dec 30 '25 04:12

songyuanyao



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!