Thread: infinite loop

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    27

    infinite loop

    anybody know what i need to do to get rid of the infinite loop?

    Code:
    do 
      {
      printf("Enter the number of tests:");
      scanf("%d", &test);
      if (test< 0 || test> 4)
      printf("Wrong number. Please try again!\n");
      } 
      while (test< 0 || test>4);

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Assuming test is of type int, there is no infinite loop in that code fragment, unless the user enters junk characters that cannot be interpreted as an integral value

    Entering a value between 0 and 4 will terminate it.

    If you want to allow the user to enter junk data and not have the program loop forever, use fgets() to read a line of input (as a character string) then check the characters before using sscanf() to parse the string.

    Alternatively, check the return value of scanf() to see if it succeeded in reading an int. If the return value indicates no integer was read, read a character, discard that character, and go again.

    Read the documentation for scanf() to get an idea of what it's return values mean.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    27
    Yeah when i run it and enter a wrong number it will keep saying Wrong number. Please try again! I need it to say that and then prompt the user to type the number in again until they type one that fits but I can't seem to figure out how with the loop.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    You are complaining about the code doing exactly what it is supposed to. It will loop until the user enters an integral value between 0 and 4.

    I've already described options for handling the user who enters rubbish characters (which would cause your code snippet, as written, to repeatedly complain about a wrong value, but not accept any further input).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Registered User
    Join Date
    Dec 2011
    Posts
    27
    Quote Originally Posted by grumpy View Post
    You are complaining about the code doing exactly what it is supposed to. It will loop until the user enters an integral value between 0 and 4.

    I've already described options for handling the user who enters rubbish characters (which would cause your code snippet, as written, to repeatedly complain about a wrong value, but not accept any further input).
    not really complaining, i came here with a problem seeking help to fix it, after all isnt this what this website is for? as for the fgets and sscanf, we havent learned those yet so i cant use those for my code. i tried a few different things with for loops and also tried changing the while loop but its still giving me that infinite loop when the user enters rubbish/wrong number.

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    If you provide the full code, and tell us the input you're giving the program when this happens, it might help us see if something else is going awry somehow. Based on what you've told us, everything should be working as expected, which makes it difficult to "fix."

    You also didn't mention whether you tried grumpy's advice in post #2, about checking the return value of "scanf()."

  7. #7
    Registered User
    Join Date
    Jan 2013
    Posts
    106
    Code:
    do 
      {
      printf("Enter the number of tests:");
      scanf("%d", &test);
      if (test< 0 || test> 4)
      printf("Wrong number. Please try again!\n");
      break;
      } 
      while (test< 0 || test>4);


    There ya go, but you still haven't handled the error.
    Now any time it jumps out you still have to check the value is within range 0-4 before you use it.

  8. #8
    Registered User
    Join Date
    Jan 2013
    Posts
    106
    Code:
    while (test< 0 || test>4) { 
      printf("Enter the number of tests:");
      scanf("%d", &test);
      if (test< 0 || test> 4)
      printf("Wrong number. Please try again!\n");
      } else {
      break;
     } // if
    } // while

  9. #9
    Registered User
    Join Date
    Dec 2011
    Posts
    27
    got everything working now ignore the post below too
    Last edited by return0; 03-01-2013 at 11:40 PM.

  10. #10
    Registered User
    Join Date
    Dec 2011
    Posts
    27
    i had this part working earlier but i changed some stuff and now i cant figure out how to get this part to work correctly again. i know its a simple problem but i guess im just burnt out because i cant find what im doing wrong!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 10-14-2011, 11:33 PM
  2. Infinite Loop
    By ASCII in forum C Programming
    Replies: 10
    Last Post: 09-03-2011, 09:11 AM
  3. why is this an infinite loop?
    By pluneb in forum C Programming
    Replies: 3
    Last Post: 09-18-2009, 02:55 PM
  4. infinite while loop
    By beon in forum C Programming
    Replies: 15
    Last Post: 12-06-2006, 01:17 PM
  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