![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Unverified User
Join Date: Sep 2005
Posts: 209
Rep Power: 0
![]() |
new program infinite loop problem
hey all,
I'm starting to write a new program, but i'm running into an infinite loop problem with just the menu layout. Here's the code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
int selection=1;
while(selection >= 1 && selection <4)
{
printf("Please select an option from the following menu:\n");
printf("1) Display the vowels in an input string.\n");
printf("2) Determine if the ceiling of the square root of an integer is odd or even.\n");
printf("3) Display the month, name, and days in that month.\n");
printf("4) Exit.\n\n");
scanf(" ", &selection);
switch(selection)
{
case '1':
printf("...\n");
break;
case '2':
printf(".....\n");
break;
case '3':
printf("......\n");
break;
case '4':
printf("........\n");
break;
default:
printf("Please retry using 1, 2, 3, or 4.\n");
}
}
system("PAUSE");
return 0;
}It'll run once and ask for a selection, but whatever you select throws seems to crash it. I don't think its picking up the scanf right above the switch. Can anyone tell me what i did wrong. I appreciate all the help guys. |
|
|
|
|
|
#2 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,209
Rep Power: 5
![]() |
I don't understand what you mean by "select throw".
The scanf() format includes no format specifiers, so will never read anything into selection. It will also enter an infinite loop if you enter any non-whitespace. I'm also guessing that the cases should be integers (1, 2, 3, 4), not characters ('1', '2', '3', 4') |
|
|
|
|
|
#3 |
|
Programmer
Join Date: Sep 2005
Posts: 50
Rep Power: 3
![]() |
Just repeating what grumpy said only i'll show you in the code:
scanf("%d"&selection); you need the %d to specify that it's an integer you're throwing into variable selection. Also: case 1: single quotes always specify a single character, in this case you declared selection as an integer but compared the character value. You need to remove those single quotes. |
|
|
|
|
|
#4 |
|
Unverified User
Join Date: Sep 2005
Posts: 209
Rep Power: 0
![]() |
OMG!
I look like the dumbest person alive. I have just come back from about a month break between semesters and I have already forgotten the syntax for scanf. OMG! That could be the reason for the error. Thanks for the help to the most dumbass question ever. I'll try not to inconvenience you as bad next time. Seriously. I do apologize for this one, guys. |
|
|
|
|
|
#5 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,209
Rep Power: 5
![]() |
Naah, you're not the dumbest person alive. I've seen dumber questions, albeit only a few
![]() |
|
|
|
|
|
#6 |
|
Programmer
Join Date: Sep 2005
Posts: 50
Rep Power: 3
![]() |
It's okay, we're all here to learn and help each other no matter how dumb the question is. Though on occassion people do get the RTFM, moreso in IRC...mainly me..but nonetheless...
|
|
|
|
|
|
#7 |
|
Expert Programmer
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4
![]() |
You want dumb, listen to this: I go to work yesterday, open my email and read one that has someone else's address in the TO field. I thought I was receiving email intended for her somehow. So I contacted the Help Desk, started a trouble ticket, and got priority over production problems. Three hours later, after thorought investigation, the help desk couldn't determine anything wrong with my or her accounts so they asked for a copy of the email.
So I went to my Inbox one more time, opened the email, and right before i hit forward i notice something i didn't before. The email was CC'ed for the whole company ![]() I'm usually smarter than that (otherwise i wouldn't have this job ), but it happens.Anyways, my new rule is: NEVER make decisions before getting your morning coffee! |
|
|
|
|
|
#8 | |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
Quote:
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for." -- Socrates |
|
|
|
|
|
|
#9 |
|
Hobbyist Programmer
|
Yea realy bad lol, i bet the help desk wasn't to happy either
|
|
|
|
|
|
#10 |
|
Expert Programmer
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4
![]() |
I never head back from them after I emailed and apologized. Yea I felt like a complete moron, I don't think I'll be asking for help anymore.
I bet they had a good laugh though because I'm still in training. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|