Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 21st, 2005, 9:35 PM   #1
Kilo
Expert Programmer
 
Kilo's Avatar
 
Join Date: Nov 2005
Location: In Pink Clam?
Posts: 542
Rep Power: 0 Kilo is an unknown quantity at this point
Send a message via AIM to Kilo
cin problem

ok i have a function inside that function i ask for user input... i throw the user input(int) into a switch case, and at some point in each case i call the function its currently in.. well it just zips right through my cin >> i_choice; line with the previous option i chose(IE - 1,2,3) Can someone tell me why this is?
__________________
"When in Rome, Do as the Romans Do"
"Beauty is in the eye of the BEER holder"
"Save your breath your going to need it for your blow up doll later"

SearchLores.org
Kilo is offline   Reply With Quote
Old Nov 21st, 2005, 9:48 PM   #2
lectricpharaoh
Caffeinated Neural Net
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 1,034
Rep Power: 5 lectricpharaoh will become famous soon enough
Quote:
Originally Posted by Kilo
ok i have a function inside that function i ask for user input... i throw the user input(int) into a switch case, and at some point in each case i call the function its currently in.. well it just zips right through my cin >> i_choice; line with the previous option i chose(IE - 1,2,3) Can someone tell me why this is?
Sounds like there was some error condition that stalled your stream. You'll continue to read the same offending item out of the stream until you a) clear the error, and b) remove the offending item from the stream.

Another possible problem could be the construction of your switch() statement. You might have an unintentional case of fall-through.
However, without seeing the offending piece of code, I can't say for certain. My telepathic probes haven't been working too well as of late.
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot.
- Vaarsuvius, Order of the Stick
lectricpharaoh is offline   Reply With Quote
Old Nov 21st, 2005, 9:57 PM   #3
Kilo
Expert Programmer
 
Kilo's Avatar
 
Join Date: Nov 2005
Location: In Pink Clam?
Posts: 542
Rep Power: 0 Kilo is an unknown quantity at this point
Send a message via AIM to Kilo
coming right up!



void displayMenu()
{
	system("cls");

	i_choice=0;

	cout << "\t\t          Text-Based Flight Simulation" << endl;
    cout << "\t================================================================" << endl;
	cout << "\t     [1] List Status(ALL)	[2] Edit Status(SINGLE)" << endl << endl;

	cout << "\t     #:";
	cin >> i_choice;

	switch(i_choice)
	{
	case 0:
		exit(1);
	case 1:
		p_Air=&rx77;
		cout << "=====" << endl;
		cout << "name:" << p_Air->getName() << endl;

		if(p_Air->getAirStatus()==0)
		{
			cout << "in flight?:no" << endl;
		} else {
			cout << "in flight?:yes" << endl;
		}

		cout << "=====" << endl << endl;

		p_Air=&f16;
		cout << "=====" << endl;
		cout << "name:" << p_Air->getName() << endl;

		if(p_Air->getAirStatus()==0)
		{
			cout << "in flight?:no" << endl;
		} else {
			cout << "in flight?:yes" << endl;
		}

		cout << "=====" << endl << endl;

		p_Air=&f18;
		cout << "=====" << endl;
		cout << "name:" << p_Air->getName() << endl;

		if(p_Air->getAirStatus()==0)
		{
			cout << "in flight?:no" << endl;
		} else {
			cout << "in flight?:yes" << endl;
		}

		cout << "=====" << endl;
		system("pause");
		displayMenu();
		break;

	case 2:
		editMenu();

		cout << "\t     #:";
		cin >> i_choice;

		switch(i_choice)
		{
		case 1:
			p_Air=&rx77;
			break;
		case 2:
			p_Air=&f16;
			break;
		case 3:
			p_Air=&f18;
		default:
			break;
		}

		editMenu2();

		cout << "\t     #:";
		cin >> i_choice;

		switch(i_choice)
		{
		case 1:
			cout << "\n\t\tChoose a name for the plane:";
			cin.getline(tmpName,30);
			cin.get();
			p_Air->setName(tmpName);
			system("pause");
			displayMenu();
			break;
		case 2:
			p_Air->editTakeOff();
			system("pause");
			displayMenu();
			break;
		default:
			break;
		}

		break;

	default:
		exit(1);
	}
}

grrr please help!
__________________
"When in Rome, Do as the Romans Do"
"Beauty is in the eye of the BEER holder"
"Save your breath your going to need it for your blow up doll later"

SearchLores.org

Last edited by Kilo; Nov 21st, 2005 at 10:22 PM.
Kilo is offline   Reply With Quote
Old Nov 21st, 2005, 10:29 PM   #4
Kilo
Expert Programmer
 
Kilo's Avatar
 
Join Date: Nov 2005
Location: In Pink Clam?
Posts: 542
Rep Power: 0 Kilo is an unknown quantity at this point
Send a message via AIM to Kilo
please help me!!!
__________________
"When in Rome, Do as the Romans Do"
"Beauty is in the eye of the BEER holder"
"Save your breath your going to need it for your blow up doll later"

SearchLores.org
Kilo is offline   Reply With Quote
Old Nov 21st, 2005, 11:02 PM   #5
lectricpharaoh
Caffeinated Neural Net
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 1,034
Rep Power: 5 lectricpharaoh will become famous soon enough
Quote:
Originally Posted by Kilo
cin >> i_choice;
 
switch(i_choice)
This bit is likely your problem. If the user types "abc" instead of "1", you have a problem. The extraction operator chokes on the bad input, and the bad input remains in the stream, waiting for the next extraction operation (which will try to retrieve the same item, whether it's valid or not for the next operation).

Between those two lines of code you have up there, you need some kind of validation. You can check using cin.good(), cin.bad(), or cin.fail(). All are Boolean functions. You may as well stick with one in your code.

If the stream is still okay, then you have no problems there (the input still might not be in the expected range, but you can perform further checking). On the other hand, if cin.good() is false, you've got bad input. You can handle it something like this:
cout << "Please enter an integer: ";
cin >> x;
while(!cin.good())
{
   cout << "\nThat's not an integer. Please enter an integer: ";
   cin.clear(); // clear the error
   cin.sync(); // remove offending data from the input stream
   cin >> x;
}
As for the rest of the code, I don't understand why you have a case 0: in there, considering you don't have that in your menu options, and you have a default: to handle things not in the menu. Also, I'd suggest changing the action of default: to something other than a program abort. Perhaps redisplaying the menu and prompting the user for a valid choice would be a better approach.
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot.
- Vaarsuvius, Order of the Stick
lectricpharaoh is offline   Reply With Quote
Old Nov 22nd, 2005, 12:38 AM   #6
Kilo
Expert Programmer
 
Kilo's Avatar
 
Join Date: Nov 2005
Location: In Pink Clam?
Posts: 542
Rep Power: 0 Kilo is an unknown quantity at this point
Send a message via AIM to Kilo
i think cin.sync is what i need... because im not having input problems. The second time around after the function has been called within itself the input is auto set to the last number entered {1,2,3}. and i have no clue why
__________________
"When in Rome, Do as the Romans Do"
"Beauty is in the eye of the BEER holder"
"Save your breath your going to need it for your blow up doll later"

SearchLores.org
Kilo is offline   Reply With Quote
Old Nov 22nd, 2005, 8:14 AM   #7
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
There's a part of lectric's post you didn't absorb enough of. You may see absolutely NOOO problems except maybe an unused newline, which can be handled by the cin.sync. One germane part of his post is this: any input mechanism that is also attempting to convert keystrokes (to a number, say) will cause a failure with bad input. YOU may not type 'a' when you're looking for a '1'. You know "what to do" (but can probably make typos anyway). Your user may be dumb, or fall asleep with his head on the keyboard, or be malicious, or have a cat who likes to prance over the keys. Always check your streams, particular input streams, for failure after an input. Anything less is the hallmark of an amateur. Once one has learned one should do so, and does not, one is a schlock.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Nov 22nd, 2005, 11:02 AM   #8
lectricpharaoh
Caffeinated Neural Net
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 1,034
Rep Power: 5 lectricpharaoh will become famous soon enough
Quote:
Originally Posted by Kilo
i think cin.sync is what i need... because im not having input problems. The second time around after the function has been called within itself the input is auto set to the last number entered {1,2,3}. and i have no clue why
Using cin.sync() by itself won't accomplish much. All it will do is get rid of characters currently in the input buffer. If the stream encountered an error, it will remain in an error state until you clear the error. You need to clear the error and clear the buffer. If you don't clear the error, the stream remains broken. If you clear the error, but don't use cin.sync() (or another technique to remove the input from the buffer), the bad input will remain in the stream, and you will read it again, causing the same error (can you say endless loop?).

The main reason I'd use cin.sync() by itself is to clear 'garbage' out of the input stream, often before displaying a menu or something, to ensure my program didn't attempt to read it.
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot.
- Vaarsuvius, Order of the Stick
lectricpharaoh is offline   Reply With Quote
Old Nov 22nd, 2005, 12:37 PM   #9
Kilo
Expert Programmer
 
Kilo's Avatar
 
Join Date: Nov 2005
Location: In Pink Clam?
Posts: 542
Rep Power: 0 Kilo is an unknown quantity at this point
Send a message via AIM to Kilo
DaWei, your are absoluty right about checking the streams i completely agree!


lectricpharaoh, cin.sync(); fixed my program! it runs perfect now. And now im going to take DaWei's advice and impliment stream checking into all of my programs. just for assurance. Thank you both very much!
__________________
"When in Rome, Do as the Romans Do"
"Beauty is in the eye of the BEER holder"
"Save your breath your going to need it for your blow up doll later"

SearchLores.org
Kilo is offline   Reply With Quote
Old Nov 22nd, 2005, 12:54 PM   #10
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
I'm going to beat a dead pony here a bit. Oddly enough, I tend to get two polar-opposite criticisms: one is that I beat dead horses; the other is that I chew new posters for being lazy or thoughtless in not reading the "How to Post..." threads. Those, among their many recommendations, suggest that it would be well for the poster to search the forum for dead horses. Oh well, here we go.

Many coders, when you suggest they are leaving out important things, respond with, "Oh, I'm going to get it working, then I'll make it good code." There are two problems with that. When one "gets it working", one tends to be through, schlocky as the result may be. The other is that, as humans, we are creatures of habit. Putting off making it good becomes rote. Consequently, it may never see "good." It is fortunate that in critical situations one is ridden constantly by bureaucratic guardians such as the FDA. Nevertheless, do you want to depend on a bureaucrat to keep you from killing a child? I'm thinking, "Probably not."

I commend you on your attitude to go ahead and do it properly.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:29 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC