Programming Forums
User Name Password Register
 

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

Reply
 
Thread Tools Display Modes
Old Jan 9th, 2006, 3:48 PM   #1
codylee270
Unverified User
 
Join Date: Sep 2005
Posts: 209
Rep Power: 0 codylee270 is an unknown quantity at this point
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.
codylee270 is offline   Reply With Quote
Old Jan 9th, 2006, 5:29 PM   #2
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,209
Rep Power: 5 grumpy is on a distinguished road
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')
grumpy is offline   Reply With Quote
Old Jan 9th, 2006, 5:39 PM   #3
Serinth
Programmer
 
Serinth's Avatar
 
Join Date: Sep 2005
Posts: 50
Rep Power: 3 Serinth is on a distinguished road
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.
Serinth is offline   Reply With Quote
Old Jan 9th, 2006, 7:35 PM   #4
codylee270
Unverified User
 
Join Date: Sep 2005
Posts: 209
Rep Power: 0 codylee270 is an unknown quantity at this point
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.
codylee270 is offline   Reply With Quote
Old Jan 9th, 2006, 7:43 PM   #5
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,209
Rep Power: 5 grumpy is on a distinguished road
Naah, you're not the dumbest person alive. I've seen dumber questions, albeit only a few
grumpy is offline   Reply With Quote
Old Jan 9th, 2006, 10:25 PM   #6
Serinth
Programmer
 
Serinth's Avatar
 
Join Date: Sep 2005
Posts: 50
Rep Power: 3 Serinth is on a distinguished road
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...
Serinth is offline   Reply With Quote
Old Jan 10th, 2006, 6:30 AM   #7
OpenLoop
Expert Programmer
 
OpenLoop's Avatar
 
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4 OpenLoop is on a distinguished road
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!
OpenLoop is offline   Reply With Quote
Old Jan 10th, 2006, 9:11 AM   #8
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
Quote:
Originally Posted by OpenLoop
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!
Haha that's quite funny, although you must have felt bad when you discovered it.
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion is offline   Reply With Quote
Old Jan 10th, 2006, 9:56 AM   #9
HaCkeR
Hobbyist Programmer
 
HaCkeR's Avatar
 
Join Date: Nov 2005
Location: UK
Posts: 131
Rep Power: 0 HaCkeR is an unknown quantity at this point
Send a message via AIM to HaCkeR Send a message via MSN to HaCkeR
Yea realy bad lol, i bet the help desk wasn't to happy either
HaCkeR is offline   Reply With Quote
Old Jan 10th, 2006, 12:04 PM   #10
OpenLoop
Expert Programmer
 
OpenLoop's Avatar
 
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4 OpenLoop is on a distinguished road
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.
OpenLoop 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 4:22 AM.

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