Thread: Infinite Loop

  1. #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.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    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)
    Last edited by Dave_Sinkula; 11-30-2004 at 04:41 PM. Reason: Added link for any FAQ-impaired visitors.
    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.*

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    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.

  4. #4
    Registered User cdonlan's Avatar
    Join Date
    Sep 2004
    Posts
    49
    Mybad it should of been 0.

    Still not working.

    thanks

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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...
    warning: format argument is not a pointer (arg2)
    Try passing the address of the variable instead of the value.

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User cdonlan's Avatar
    Join Date
    Sep 2004
    Posts
    49
    No warning was given.

    Thanks

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Turn them on then. Because your life will be a whole lot easier if you use them.

    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    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.

  9. #9
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    [edit]
    try -Wall as a compiler switch
    [/edit]
    Last edited by prog-bman; 11-30-2004 at 07:38 PM.
    Woop?

  10. #10
    Registered User cdonlan's Avatar
    Join Date
    Sep 2004
    Posts
    49
    The warnings are on......

  11. #11
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Then get a better compiler that will warn you of such mistakes.

  12. #12
    Registered User cdonlan's Avatar
    Join Date
    Sep 2004
    Posts
    49
    Thanks...lots of help

  13. #13
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    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.

  14. #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

  15. #15
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    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?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 06-14-2009, 11:24 PM
  2. Cosine fucntion and infinite loop.
    By youareafever in forum C Programming
    Replies: 2
    Last Post: 11-07-2008, 04:45 AM
  3. Infinite Loop with GetAsyncKeyState
    By guitarist809 in forum Windows Programming
    Replies: 1
    Last Post: 04-18-2008, 12:09 PM
  4. Switch statement = infinite loop
    By Lucid003 in forum C++ Programming
    Replies: 10
    Last Post: 10-10-2005, 12:46 AM
  5. stays in loop, but it's not an infinite loop (C++)
    By Berticus in forum C++ Programming
    Replies: 8
    Last Post: 07-19-2005, 11:17 AM