954,505 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

error with int cin

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.
[/CODE]
int num;
cout<<"Enter a number: ";
while(!(cin>>num) && !cin.eof())
{
cout<<"Error\n";
cin.clear();
cout<<"Please enter a number again: ";
cin>>num;
}[/CODE]

wjyeo
Newbie Poster
6 posts since Aug 2007
Reputation Points: 10
Solved Threads: 0
 

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)

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

> 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'

vijayan121
Posting Virtuoso
1,606 posts since Dec 2006
Reputation Points: 1,159
Solved Threads: 287
 

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.

Attachments test_result1.JPG 49.28KB test_result2.JPG 49.38KB
wjyeo
Newbie Poster
6 posts since Aug 2007
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You