![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Expert Programmer
|
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 |
|
|
|
|
|
#2 | |
|
Caffeinated Neural Net
![]() Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 1,034
Rep Power: 5
![]() |
Quote:
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 |
|
|
|
|
|
|
#3 |
|
Expert Programmer
|
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. |
|
|
|
|
|
#4 |
|
Expert Programmer
|
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 |
|
|
|
|
|
#5 | |
|
Caffeinated Neural Net
![]() Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 1,034
Rep Power: 5
![]() |
Quote:
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;
}
__________________
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 |
|
|
|
|
|
|
#6 |
|
Expert Programmer
|
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 |
|
|
|
|
|
#7 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#8 | |
|
Caffeinated Neural Net
![]() Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 1,034
Rep Power: 5
![]() |
Quote:
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 |
|
|
|
|
|
|
#9 |
|
Expert Programmer
|
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 |
|
|
|
|
|
#10 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|