Go Back   Forums > Community Chatterbox > Tech Corner > Programming
Memberlist Forum Rules Today's Posts
Search Forums:
Click here to use Advanced Search

Reply
 
Thread Tools Display Modes
Old 05-12-2009, 12:49 AM   #1
Acero
Newbie
 
Acero's Avatar

 
Join Date: May 2009
Location: Olean, United States
Posts: 20
Default

EL Quia is correct in their response. Validating the user input is always a requirement.

To expand upon the answer, when the user of your program enters a character into the iostream object(cin) of type int, the int is filled with garbage data and the failbit of the iostream object is set to true. Until the iostream object failbit is cleared and the data in the cin stream is dicarded, your cin is unusable and will not get user input again, hence your infinite loop.

Here is an example of something similar to what you might want to try:

Code:
unsigned short int taketarget (unsigned short int records)
     {  
        int target=0;
        
       while (target<1 || target>records)
         { 
			cout << "\nEnter target record number:\n";
			cin >> target;
			if (cin.fail())
			{
				cout << "Invalid Input." << endl;
				cin.clear();
				cin.ignore();
				cout << "Enter a number between 1 and " << records << ".\n";
			}
         }
        return target;
     }
I hope this helps you
Acero is offline                         Send a private message to Acero
Reply With Quote
Old 05-12-2009, 10:31 AM   #2
The Fifth Horseman
FUTURE SCIENCE BASTARD
 
The Fifth Horseman's Avatar


 
Join Date: Oct 2004
Location: Opole, Poland
Posts: 14,276
Default

Thanks, Acero. This solved the problem perfectly.
__________________

"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
Old 05-12-2009, 01:04 PM   #3
Acero
Newbie
 
Acero's Avatar

 
Join Date: May 2009
Location: Olean, United States
Posts: 20
Default

I'm quite happy to have been of help. May I ask what kind of project this code was for? School work? Working on a game?

Quote:
Originally Posted by The Fifth Horseman View Post
Thanks, Acero. This solved the problem perfectly.
Acero is offline                         Send a private message to Acero
Reply With Quote
Old 05-12-2009, 02:05 PM   #4
The Fifth Horseman
FUTURE SCIENCE BASTARD
 
The Fifth Horseman's Avatar


 
Join Date: Oct 2004
Location: Opole, Poland
Posts: 14,276
Default

A tool to convert sprites from Space Hulk's non-standard format to PCX.
__________________

"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
Reply


Similar Threads
Thread Thread Starter Forum Replies Last Post
Killer Loop [SOLD] Paco Rejected requests 2 04-08-2011 04:47 PM
X-COM 1 (UFO) Saving game locks up - Dosbox Thraka Troubleshooting 3 19-06-2008 12:10 AM
Infinite Dungeons For Nwn Lucullus Gaming Zone 13 20-06-2006 12:23 PM
Gauntlet 2 System Loop insidious Troubleshooting 7 18-05-2006 08:49 AM
Eternam - computer locks up wendymaree Troubleshooting 6 02-08-2004 08:43 AM


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump
 


The current time is 12:34 PM (GMT)

 
Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.