what's wrong with this? neverending loop

This is a discussion on what's wrong with this? neverending loop within the C++ Programming forums, part of the General Programming Boards category; hi, trying to make a small block of code for trapping input errors, I wrote this: #ifdef HAVE_CONFIG_H #include <config.h> ...

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    7

    what's wrong with this? neverending loop

    hi, trying to make a small block of code for trapping input errors, I wrote this:


    #ifdef HAVE_CONFIG_H
    #include <config.h>
    #endif
    #include <iostream.h>
    #include <stdlib.h>

    int main(int argc, char *argv[])
    {

    int pirla;
    do
    {
    cout << "Inserisci un valore intero: ";
    cin >> pirla;
    }
    while (cin.good() == false);
    cout << "Il valore di pirla e' " << pirla << endl;
    return EXIT_SUCCESS;
    }

    but if the input is wrong the do...while loop never ends keeping outputting the prompt.
    why this happens? how can I put that to work?
    thanks and... mercy for the newbie!

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    155
    I think you have to call cin.clear() after any call to cin.good() or cin.fail(). I can't explain why, something about a fail bit that needs to be cleared. If it isn't cleared cin won't accept input the second time around so good() fails and away you go. I would probably use a flag to get around this, although there are probably other ways as well. I would try this


    char flag = 'y';

    do
    {
    cout << "Inserisci un valore intero: ";
    cin >> pirla;
    if(cin.good())
    {
    cin.clear();
    flag = 'n';
    }
    else
    {
    cin.clear();//this may not be necessary but I'd try it this way first
    cout << "error. try again." << endl;
    }
    while (flag == 'y');
    cout << "Il valore di pirla e' " << pirla << endl;

  3. #3
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    ..and you'll have to remove the bad input from the stream. Try something like this -

    Code:
    int pirla;
    	cout << "Inserisci un valore intero: ";
    	cin >> pirla;
    	while(cin.fail())
    	{
    		cin.clear();
    		cin.ignore(80,'\n');
    	
    		cout << "Inserisci un valore intero: ";
    		cin >> pirla;
    	}
    	
    	cout << "Il valore di pirla e' " << pirla << endl;
    zen

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 10-27-2006, 01:21 PM
  2. neverending loop
    By bazzano in forum C Programming
    Replies: 6
    Last Post: 09-19-2005, 08:29 AM
  3. How to change recursive loop to non recursive loop
    By ooosawaddee3 in forum C Programming
    Replies: 1
    Last Post: 06-24-2002, 08:15 AM
  4. What is wrong with my while loop?
    By aspand in forum C Programming
    Replies: 3
    Last Post: 06-19-2002, 12:07 PM
  5. Whats wrong w/ my loop?? Please help?
    By aspand in forum C Programming
    Replies: 6
    Last Post: 05-30-2002, 03:42 AM

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