Hi, i try to get an input between 1 and 100, if user enters anything else other than that the program shud allow user to enter another number so the processes will keep going without restarting program. What should i use to make this?
Hi, i try to get an input between 1 and 100, if user enters anything else other than that the program shud allow user to enter another number so the processes will keep going without restarting program. What should i use to make this?
A bit of logical thinking would be a good starting point. You need something that can prompt the user for input, read input from the user, check the input, and loop until the input is valid.
The second step for you will be a bit of reading up on C syntax and library functions that do parts of the above. The introductory sections of all basic texts on C (even the really bad ones) will get you started.
You and Quzah do this "How to Fish" thing, so well! <bow>
This code tests condition but which function after that make user to enter n again if 'DONE' have not written?Code:int n,k; printf("Enter A Number Lower Than 100: "); scanf("%d", &n);while(n<100){ printf("DONE\n"); break; }
Isn't while(1) bad practice?
Code:while(!(n>=1 && n<=100)) { printf("Enter n"); scanf("%d",&n); }
Operating Systems:
- Ubuntu 9.04
- XP
Compiler: gcc
I would use do-while instead - that way, there's no problem with whatever n happens to be before the input - and it makes sure that the reader understands that "we do this at least once, maybe more times", rather than the usual "while" which is "zero or more times".
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
Thanks for the answers. Im gonna try to make it with do-while as matsp says its more useful.