Thread: How to set conditions for a input?

  1. #1
    Registered User QuaX's Avatar
    Join Date
    Apr 2009
    Posts
    12

    Exclamation How to set conditions for a input?

    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?

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    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.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You and Quzah do this "How to Fish" thing, so well! <bow>

  4. #4
    Registered User QuaX's Avatar
    Join Date
    Apr 2009
    Posts
    12
    Code:
    int n,k;
    printf("Enter A Number Lower Than 100:  ");
    scanf("%d", &n);while(n<100){
            printf("DONE\n");
            break;
            }
    This code tests condition but which function after that make user to enter n again if 'DONE' have not written?

  5. #5
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by QuaX View Post
    Code:
    int n,k;
    printf("Enter A Number Lower Than 100:  ");
    scanf("%d", &n);while(n<100){
            printf("DONE\n");
            break;
            }
    This code tests condition but which function after that make user to enter n again if 'DONE' have not written?
    i think this is what u're looking for.
    Code:
    #include<stdio.h>
    int main(void)
    {
    	int n;
    	while(1)
    	{
    		printf("Enter n");
    		scanf("%d",&n);
    		if(n>=1 && n<=100)
    		{
    			printf("DONE");
    			break;
    		}
    	}
    	getch();
    	return 0;
    }

  6. #6
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312
    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

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Ideswa View Post
    Isn't while(1) bad practice?

    Code:
    while(!(n>=1 && n<=100))
    {
    	printf("Enter n");
    	scanf("%d",&n);
    }
    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.

  8. #8
    Registered User QuaX's Avatar
    Join Date
    Apr 2009
    Posts
    12
    Thanks for the answers. Im gonna try to make it with do-while as matsp says its more useful.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  2. Trouble with a lab
    By michael- in forum C Programming
    Replies: 18
    Last Post: 12-06-2005, 11:28 PM
  3. LISP (DrScheme) any one?
    By Jeremy G in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 03-31-2004, 12:52 PM
  4. My graphics library
    By stupid_mutt in forum C Programming
    Replies: 3
    Last Post: 11-26-2001, 06:05 PM