View Single Post
Old 02-12-2009, 10:07 PM   #1
The Fifth Horseman
FUTURE SCIENCE BASTARD
 
The Fifth Horseman's Avatar


 
Join Date: Oct 2004
Location: Opole, Poland
Posts: 14,276
Default Function locks into an infinite loop: why?

Okay, I have a program that moves across a bunch of records stored into memory and reports information about each (number, adress, some initial values).
Full sourcecode


It (mostly) works, but the following function doesn't work as intended.
Code:
unsigned short int taketarget (unsigned short int records)
     {  
        int target=0;
        cout << "\nEnter target record number:\n";
       while (target<1 || target>records)
         { cin >> target;
         if (target<1 || target>records)
           { cout << "Enter a number between 1 and " << records << ".\n"; }
         }
        return target;
     }
It's supposed to keep prompting the user until valid input is entered.
It does just fine if the user enters numeric values - but if a character is entered, the function loops infinitely without giving the user any way to enter new input.

What may be the cause of this behavior?
__________________

"God. Can't you people see I'm trying to commit a crime against science and nature here?"
-- Reed Richards
The Fifth Horseman is offline                         Send a private message to The Fifth Horseman
Reply With Quote