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);
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);
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.
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.
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.
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()."
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.
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
got everything working now ignore the post below too
Last edited by return0; 03-01-2013 at 11:40 PM.
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!