|
|
Or, use your gamerDNA username: (more...)
| ||||||
| |
![]() |
| | LinkBack | Thread Tools | Rate Thread | Display Modes |
| | #1 (permalink) | |
| Registered User Join Date: Jan 2002
Posts: 1,918
| C Switch Statement This is driving me crazy. Basically, it just skips past the the if statement and doesn't get a character. I'm sure it's something ridiculously simple I'm doing incorrectly. Quote:
Code: switch(selection) {
case 1:
printf("Are you sure you would like to enter new data? Previous values will be erased (y or n): ");
yn = getchar();
if (yn == 'y')
{
test();
}
break; Code: #include | |
| | |
| | #2 (permalink) |
| Registered User Join Date: Nov 2003
Posts: 1,622
| My only assumption is int selection is not being properly set. Does it only fail on case 1 or does it fail on every case? If it fails for everything then I don't understand why selection is global to begin with in the first place. It's scope should be reduced to only the main() method. Code: int selection = scan();
switch(selection) {
case 1:
printf("Are you sure you would like to enter new data? Previous values will be erased (y or n): ");
yn = getchar();
if (yn == 'y')
{
test();
}
break; |
| | |
| | #3 (permalink) | |
| Registered User Join Date: Jan 2002
Posts: 1,918
| Quote:
| |
| | |
| | #4 (permalink) |
| Fires of Heaven WoW Officer Join Date: Jul 2006 Location: Irvine, CA
Posts: 202
| The getchar() is returning the leftover newline character from the previous input. You can substitute the getchar() line with something like Code: do {yn = getchar();} while (yn <= 32); P.S. Disclaimer, this is just a quick fix/hack to your program, and not how I would do it ![]() Last edited by Araex; 04-13-2009 at 12:29 PM.. |
| | |
| | #5 (permalink) |
| So there's this plane on a treadmill... Join Date: Jan 2005 Location: Southern California
Posts: 3,217
+20 Internets | nm thats probably it. =)
__________________ " We are all atheists about most of the gods that societies have ever believed in. Some of us just go one god further." Richard Dawkins (1941 - ), "The Root of All Evil", UK Channel 4, 2006 Last edited by Zuuljin; 04-13-2009 at 12:19 PM.. |
| | |
| | #6 (permalink) |
| Registered User Join Date: Nov 2003
Posts: 1,622
| Is the issue getchar() does not set yn to a proper variable? I am not a C programmer by any stretch but from my quick google search it appears like getchar() is used to read a single char from a file and you may want to just use a scanf here. -edit- nm looks like a real C programmer answered |
| | |
| | #7 (permalink) | |
| Registered User Join Date: Jan 2002
Posts: 1,918
| Quote:
![]() Edit: Props to everyone for the help! Last edited by prescient63; 04-13-2009 at 12:36 PM.. | |
| | |
![]() |
|
| Thread Tools | |
| Display Modes | Rate This Thread |
| |