![]() |
| | #1 |
| Registered User Join Date: Sep 2004
Posts: 49
| Infinite Loop 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;
}
}
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 | |
| | #2 |
| Just Lurking 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 | |
| | #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 | |
| | #4 |
| Registered User Join Date: Sep 2004
Posts: 49
| Mybad it should of been 0. Still not working. thanks |
| cdonlan is offline | |
| | #5 | |
| +++ OK NO CARRIER 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:
Quzah.
__________________ Hundreds of thousands of dipshits can't be wrong. Are you up for the suck? | |
| quzah is offline | |
| | #6 |
| Registered User Join Date: Sep 2004
Posts: 49
| No warning was given. Thanks |
| cdonlan is offline | |
| | #7 |
| +++ OK NO CARRIER 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 | |
| | #8 |
| Deleting... 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 | |
| | #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 | |
| | #10 |
| Registered User Join Date: Sep 2004
Posts: 49
| The warnings are on...... |
| cdonlan is offline | |
| | #11 |
| & the hat of GPL slaying Join Date: Sep 2001
Posts: 5,732
| Then get a better compiler that will warn you of such mistakes. |
| Thantos is offline | |
| | #12 |
| Registered User Join Date: Sep 2004
Posts: 49
| Thanks...lots of help |
| cdonlan is offline | |
| | #13 |
| & the hat of GPL slaying 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 | |
| | #14 |
| Registered User 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 | |
| | #15 |
| & the hat of GPL slaying 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);
|
| Thantos is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |