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

Reply
 
Thread Tools Display Modes
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
Old 03-12-2009, 03:08 AM   #2
El Quia
Abandonia Homie
 
El Quia's Avatar


 
Join Date: Oct 2005
Location: Capital Federal, Argentina
Posts: 582
Default

I haven't really read all the code, and I am feeling somewhat sleepy and I am about to hit the sack, so maybe I am talking nonsense, but I think I see which is the problem. But, I can also be messing things up because of my ignorance about c++

I think the problem is that you are putting the user input into an int. And then evaluating that input (an int) and pretending that the program realized when it is really a number and when it is another thing. Each character the user is inputing goes to the int as a group of chars, so I think that messes up everything. In these cases I think it is better to take the user input and put it into a string and then parse the string first to check if it is a numeric value or if it is anything else. And only if it is a numeric value, I would check if it falls between the values required. I don't know how many parsing functions the string object has in C++, so maybe it is something of a mess to do that. But assuming that the user will input a number is always a dangerous presumption. ALWAYS try the user input as typed by a moronic child hitting the keyboard in anger. Most of the time, you will be right

I got some curiosity at you checking twice in each loop at the condition (target<1 || target>records). Maybe it is a necessity here, maybe there is another way to express that without redundancy. But I can't decide now, as I am really too sleepy.

Well, I hope I could help you, this time . And if I wasn't of help, then blame my sleep deprivation! (which is odd, because I want' really sleep deprived, today...)
El Quia is offline                         Send a private message to El Quia
Reply With Quote
Old 05-12-2009, 12:49 AM   #3
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   #4
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   #5
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   #6
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

Thread Tools
Display Modes

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 06:06 PM (GMT)

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