Hey guys i need help over in my program. My error is that when i use cin for a int declaration, when i type in a char, they straight away have a infinite loop. I try using the cin.clear() but when i prompt the user to "enter a number again" and place a cin>>num, the program just wont stop at the cin and proceed wth the rest of it or resulting in a error.
Here is the program.

int num;
cout<<"Enter a number: ";
while(!(cin>>num) && !cin.eof())
    {
        cout<<"Error\n";
        cin.clear();
        cout<<"Please enter a number again: ";
        cin>>num;
    }

Recommended Answers

All 3 Replies

Member Avatar for iamthwee

Untested...

#include <iostream>
#include <string>
#include <sstream>
#include <limits>

using namespace std;

class Crap
{
public:
  bool myCheck(string var)
  {
    int counter = 0;
    int s = var.length();
    for ( int i = 0; i < s; i++ )
    {
      if ( isNumber(var[i]) == true )
      {
        counter = counter + 1;
      }
    }
    if ( ( counter == s ) && ( var[0] != '0' ) )
      return true;
    else
      return false;
  }
  bool isNumber( char ch )
  {
   if ((ch >= '0') && (ch <= '9'))
     return true;
   else
     return false;
  }
};

int main()
{
  string input;
  

  Crap foo; 
  
  do
  {
    cout << "Enter a number ugly:";
    getline ( cin, input, '\n' );
  
    if ( foo.myCheck( input ) == true )
    {
      cout << "That's a number" << endl;
      //use stringstream to convert to
      //integer
    }
    else
     cout << "Ain't no number" << endl;
  }while ( foo.myCheck( input ) != true );
  return 0;
}

Maybe you could use limits as well. By number I assume you mean integer ( whole number)

> when i type in a char, they straight away have a infinite loop. I try using the cin.clear() ...
the problem is that there is some char(s) left in the input buffer which gave the error earlier and will continue to give errors till you remove it (them). eg.

int num = 0 ;
  while( !(cin>>num) )
  {
    cin.clear() ;
    char c ;
    cin >> c >> ws ;
  }
  cout << num << '\n' ;

for more details, search this forum for a thread which has 'flush input buffer narue'

ok thanks for the reply. Actually I'm still not familiar wth using of class. however i did manage to solve this cin issue by using atoi. however it appear another error. when i prompt the user to key in integer, at the 1st prompt if i key in int, the program will run as normal. However if i key in a char at 1st, it will prompt the 2nd time to key in a number. but the problem come here when i enter a int, it will prompt the question again and when i didnt type anything but just press enter, it will then process.

here will be the code.

cout<<"Enter a number between 1 to 20: ";
	getline(cin, line);
	num = atoi(line.c_str());
	while(num<=0 || num>20)
	{
		cout<<"Error\n";
		cout<<"Please enter a number again between 1 to 20: ";
		getline(cin, line);
		num = atoi(line. c_str()); //convert the string to integer. If alphabet, num will be 0.
	}

i also add in the output1 and 2 to show the different.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.