spot the error

This is a discussion on spot the error within the C++ Programming forums, part of the General Programming Boards category; I use the following to get two integers from user input, and also to check they are actually numbers...now it ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    17

    Question spot the error

    I use the following to get two integers from user input, and also to check they are actually numbers...now it works fine if I enter an incorect number (say the first number) and asks me to re-enter, however if I enter an invalid number again then it goes into an eternal loop. But I cant spot the problem?

    Any help or a solution would be much appreciated.

    Code:
    void userInput(char value[])
    {
    	int number1, number2;
    	cout << "Please enter a number you wish to " << value << ":" << endl;
    	cin >> number1;
    	while (!cin >> number1)
    	{
    		cout << "Error! You have entered an invalid number, " << flush;
    	// clear the state of cin, otherwise the while condition loops indefinently.
    		cin.clear();
    		cin.ignore(INT_MAX, '\n');
    		cout << "Please re-enter a number:" << endl;
    		cin >> number1;
    	}
    	cout << "Please enter a number you wish to " << value  << " " << number1 << " by" << endl;
    
    	cin >> number2;
    
    	while (!cin >> number2)
    	{
    		cout << "Error! You have entered an invalid number, " << flush;
    		cin.clear();
    		cin.ignore(INT_MAX, '\n');
    			cout << "Please re-enter a number:" << endl;
    		cin >> number2;
    	}

  2. #2
    Registered User Unreg1stered's Avatar
    Join Date
    Jul 2002
    Posts
    25
    PHP Code:
    while (!cin >> number1) {


    Shouldn't it be

    PHP Code:
    while (!number1) {


    since you already got the value of number1 from the cin >> number1; above that.

    Correct me if I'm wrong.

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    17
    Your way seems to produce the same error too so there must be something wrong possibly elsewhere Ill keep looking in the code
    Last edited by razza; 08-01-2002 at 02:43 AM.

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    17
    If anyone is interested in the whole code (not that long).
    a colour coded, spaced link is available here

  5. #5
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    cool ... do you own your own domain? How much did you pay for it?

  6. #6
    Unregistered
    Guest
    Code:
    int num1, num2;
    int flag = 0;
    while(flag == 0)
    {
       cout  << "enter an integer value for num1" << endl;
       cin >> num1;
       if(cin.fail())
       {
          cout << "data entry error.  try again" << endl;
          cin.clear();
       }
      else
          flag == 100;
    }
    
    flag = 0;
    //now repeat code for num2;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 09:18 PM
  2. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 04:35 PM
  3. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM
  4. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 09:39 AM
  5. Why wont my function exit correctly?
    By LightsOut06 in forum C Programming
    Replies: 2
    Last Post: 10-09-2005, 10:23 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21