|
![]() |
|
Thread Tools | Display Modes |
![]() |
#1 | ||
Join Date: Oct 2004
Location: Opole, Poland
Posts: 14,276
|
![]() 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).
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 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? |
||
![]() ![]() |
|
![]() |
#2 | ||
Join Date: Oct 2005
Location: Capital Federal, Argentina
Posts: 582
|
![]() 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 |
||
![]() ![]() |
|
![]() |
#3 | ||
![]() ![]() ![]() ![]() ![]() Join Date: May 2009
Location: Olean, United States
Posts: 20
|
![]() 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; } |
||
![]() ![]() |
|
![]() |
#4 | ||
Join Date: Oct 2004
Location: Opole, Poland
Posts: 14,276
|
![]() Thanks, Acero. This solved the problem perfectly.
|
||
![]() ![]() |
|
![]() |
#5 | ||
![]() ![]() ![]() ![]() ![]() Join Date: May 2009
Location: Olean, United States
Posts: 20
|
![]() |
||
![]() ![]() |
|
![]() |
#6 | ||
Join Date: Oct 2004
Location: Opole, Poland
Posts: 14,276
|
![]() A tool to convert sprites from Space Hulk's non-standard format to PCX.
|
||
![]() ![]() |
|
![]() |
|
![]() |
||||
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 |
|
|
||
  |