Infinite looping

This is a discussion on Infinite looping within the C Programming forums, part of the General Programming Boards category; Hello all. I've encountered a problem with some code I'm using and I was looking for some help. The code ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    18

    Infinite looping

    Hello all. I've encountered a problem with some code I'm using and I was looking for some help. The code is:

    Code:
    temp = 0;
    while (temp < 1 || temp > 7) {
        system("CLS");
        printf("Race selection\n--------------\n");
        printf("1. Human\n2. Dwarf\n3. Elf\n4. Gnome\n"
            "5. Half-Elf\n6. Half-Orc\n7. Halfling\n");
        printf("Race: ");
        scanf("%i", &temp);
    }
    My problem is that if the user enters a letter (I tested 'a'), then the program loops infinitely. This is not the case if the user enters numbers, as far as I can see. When numbers are entered the form re-appears as expected.

    Any help is appreciated

  2. #2
    Awesomefaceradcore bivhitscar's Avatar
    Join Date
    Apr 2006
    Location
    Melbourne, Australia
    Posts
    210
    Quote Originally Posted by InvertedSaint
    Hello all. I've encountered a problem with some code I'm using and I was looking for some help. The code is:

    Code:
    temp = 0;
    while (temp < 1 || temp > 7) {
        system("CLS");
        printf("Race selection\n--------------\n");
        printf("1. Human\n2. Dwarf\n3. Elf\n4. Gnome\n"
            "5. Half-Elf\n6. Half-Orc\n7. Halfling\n");
        printf("Race: ");
        scanf("%i", &temp);
        fflush( stdin );
    }
    My problem is that if the user enters a letter (I tested 'a'), then the program loops infinitely. This is not the case if the user enters numbers, as far as I can see. When numbers are entered the form re-appears as expected.

    Any help is appreciated

    You need to flush the input buffer using fflush.

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    18
    It works perfectly Thank you for your help

    I'll go read up on what fflush is doing, now

  4. #4
    Awesomefaceradcore bivhitscar's Avatar
    Join Date
    Apr 2006
    Location
    Melbourne, Australia
    Posts
    210
    No worries. And I noticed you are using %i in your scanf. If you're scanning for integers, that should be %d. Unless %i is an old implementation?

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,337
    > I'll go read up on what fflush is doing, now
    Say for example
    http://faq.cprogramming.com/cgi-bin/...&id=1043284351

    What you should really be doing is not using scanf() at all for reading input, but using fgets() to read the line, then use sscanf() to convert it. Both these functions return indications of success / failure.

    > Unless %i is an old implementation?
    Perfectly valid to use this instead of %d.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Awesomefaceradcore bivhitscar's Avatar
    Join Date
    Apr 2006
    Location
    Melbourne, Australia
    Posts
    210
    Quote Originally Posted by Salem
    > Unless %i is an old implementation?
    Perfectly valid to use this instead of %d.

    Ahh ok, I have never seen %i before.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. wierd looping effect after exporting 3ds to .x annimation
    By Anddos in forum Game Programming
    Replies: 3
    Last Post: 01-06-2009, 12:43 PM
  2. Infinite Looping Program Prob's
    By dld333 in forum C++ Programming
    Replies: 8
    Last Post: 09-21-2005, 12:44 AM
  3. infinite looping when 2 large integers divide
    By Horse22 in forum C++ Programming
    Replies: 5
    Last Post: 01-08-2005, 10:25 AM
  4. Looping questions
    By Peyote in forum C++ Programming
    Replies: 3
    Last Post: 09-15-2003, 11:01 PM

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