C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 11-30-2004, 04:08 PM   #1
Registered User
 
cdonlan's Avatar
 
Join Date: Sep 2004
Posts: 49
Infinite Loop

Hey guys im getting an Infinite Loop and i cannot figure out the problem here is how the code goes.

Code:
int endflag=0;
int choice;


while( endflag ==0)
{
     printf("enter choice\n");
     scanf("%d", choice);

     switch(choice)
     {
          case 1: /* function call */
                        break;

           case 2: /* another function call */
                       break;

           case 3: printf(".....")
                       endflag =1;
                       break;
           
          default: printf("please enter anouther choice")
                        break;
      }
}
It wont stop printf enter choice: please enter anouther choice.

Any ideas,
Chris

edit! sorry it was set as zero. Its fine until the default case.

Last edited by cdonlan; 11-30-2004 at 06:15 PM.
cdonlan is offline   Reply With Quote
Old 11-30-2004, 04:13 PM   #2
Just Lurking
 
Dave_Sinkula's Avatar
 
Join Date: Oct 2002
Posts: 5,006
Ah, scanf bites another. Look in the FAQ for ways to obtain numbers.

FAQ > How do I... (Level 1) > How do I get a number from the user (C)
__________________
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*

Last edited by Dave_Sinkula; 11-30-2004 at 04:41 PM. Reason: Added link for any FAQ-impaired visitors.
Dave_Sinkula is offline   Reply With Quote
Old 11-30-2004, 04:13 PM   #3
Registered User
 
Join Date: Sep 2004
Location: California
Posts: 3,029
well with the code you posted, execution never enters the while loop.

Edit: poster changed his code so that it enters loop

Last edited by bithub; 11-30-2004 at 04:17 PM.
bithub is offline   Reply With Quote
Old 11-30-2004, 06:15 PM   #4
Registered User
 
cdonlan's Avatar
 
Join Date: Sep 2004
Posts: 49
Mybad it should of been 0.

Still not working.

thanks
cdonlan is offline   Reply With Quote
Old 11-30-2004, 06:39 PM   #5
+++ OK NO CARRIER
 
quzah's Avatar
 
Join Date: Oct 2001
Posts: 11,292
That's because you're not using scanf right. scanf requires you pass it pointers as its arguments. You aren't. If you had actually paid attention to your compiler's warnings...
Quote:
warning: format argument is not a pointer (arg2)
Try passing the address of the variable instead of the value.

Quzah.
__________________
Hundreds of thousands of dipshits can't be wrong.


Are you up for the suck?
quzah is offline   Reply With Quote
Old 11-30-2004, 06:41 PM   #6
Registered User
 
cdonlan's Avatar
 
Join Date: Sep 2004
Posts: 49
No warning was given.

Thanks
cdonlan is offline   Reply With Quote
Old 11-30-2004, 07:19 PM   #7
+++ OK NO CARRIER
 
quzah's Avatar
 
Join Date: Oct 2001
Posts: 11,292
Turn them on then. Because your life will be a whole lot easier if you use them.

Quzah.
__________________
Hundreds of thousands of dipshits can't be wrong.


Are you up for the suck?
quzah is offline   Reply With Quote
Old 11-30-2004, 07:26 PM   #8
VOX
Deleting...
 
VOX's Avatar
 
Join Date: Oct 2004
Location: VA
Posts: 94
Maybe he isn't using your compiler, hmm? I don't get that warning.
VOX is offline   Reply With Quote
Old 11-30-2004, 07:33 PM   #9
Sweet
 
Join Date: Aug 2002
Location: Tucson, Arizona
Posts: 1,758
[edit]
try -Wall as a compiler switch
[/edit]
__________________
Woop?

Last edited by prog-bman; 11-30-2004 at 07:38 PM.
prog-bman is offline   Reply With Quote
Old 11-30-2004, 07:40 PM   #10
Registered User
 
cdonlan's Avatar
 
Join Date: Sep 2004
Posts: 49
The warnings are on......
cdonlan is offline   Reply With Quote
Old 11-30-2004, 07:42 PM   #11
& the hat of GPL slaying
 
Thantos's Avatar
 
Join Date: Sep 2001
Posts: 5,732
Then get a better compiler that will warn you of such mistakes.
Thantos is offline   Reply With Quote
Old 11-30-2004, 07:57 PM   #12
Registered User
 
cdonlan's Avatar
 
Join Date: Sep 2004
Posts: 49
Thanks...lots of help
cdonlan is offline   Reply With Quote
Old 11-30-2004, 08:00 PM   #13
& the hat of GPL slaying
 
Thantos's Avatar
 
Join Date: Sep 2001
Posts: 5,732
Think about it this way: If you have a saw, that is old, rusted, and is missing half of it's teeth you'd buy a new saw. The compiler is the same way. If its not warning you of simple mistakes as type mismatch for scanf() and printf() then its missing some teeth and needs to be replaced.

So yes my post is helpful.
Thantos is offline   Reply With Quote
Old 11-30-2004, 08:05 PM   #14
Registered User
 
cdonlan's Avatar
 
Join Date: Sep 2004
Posts: 49
I understand the complier im using is not the best, and a little out dated. Regardless it is the one required by my professor.

So back to the question at hand....

Should i not use scanf or should i use some type of point to the switch.

Chris
cdonlan is offline   Reply With Quote
Old 11-30-2004, 08:08 PM   #15
& the hat of GPL slaying
 
Thantos's Avatar
 
Join Date: Sep 2001
Posts: 5,732
Use the address of operator (&) before the variable to pass a point to scanf
Code:
scanf("%d", &choice);
Can we get the name, email, and phone number of your professor so we may call and yell at him for using bad compilers?
Thantos is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to stop an infinite loop by means of any key? Please help. MarkSquall C Programming 2 06-14-2009 11:24 PM
Cosine fucntion and infinite loop. youareafever C Programming 2 11-07-2008 04:45 AM
Infinite Loop with GetAsyncKeyState guitarist809 Windows Programming 1 04-18-2008 12:09 PM
Switch statement = infinite loop Lucid003 C++ Programming 10 10-10-2005 12:46 AM
stays in loop, but it's not an infinite loop (C++) Berticus C++ Programming 8 07-19-2005 11:17 AM


All times are GMT -6. The time now is 12:09 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22