Thread: Questions With Comparison

  1. #1
    Registered User
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    27

    Questions With Comparison

    pls help me, i am making a do while loop which wont accept invalid inputs..

    these invalid inputs include outside the range of 1-100, negative numbers, and zero, also i would like my loop not to accept floats, negative floats, strings and characters..

    is that possibel? i dont have an idea how to do the latter part, involving not accepting floats ang strings and character..

    here's what i've gotten to so far

    Code:
    /* gets the user's bet */
         do 
         { printf ("Your bet: $ ");
            scanf ("%d", &nBet);
         }
         while (nBet < 1 || nBet > 100);
         printf ("\n");
    thanx!

  2. #2
    Eager young mind
    Join Date
    Jun 2006
    Posts
    342
    so, u want only int right?? and that too in the range..
    I think u can check the size of the input and use it to decide wheter to enter he loop or not.. so, once in the loop ,u know u have only ints.. then check if the input is > 0 or is in hte range or what not
    but , if u want to allow the user to have the flexibility to enter any type of data.. hy are u using scanf( "%d"...)

  3. #3
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    Ok, did you test your code in any way ? Because if IIRC, scanf will not scan this that don't match it's format string (aka floats and strings)
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  4. #4
    Registered User
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    27
    i did test the code and when i enter a letter or a string or a float, it becomes an endless loop

  5. #5
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    Ok, then what you could do is use getchar() and check each character that you get with isdigit(), then convert the whole result to an actual integer
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  6. #6
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Quote Originally Posted by jarvis
    pls help me, i am making a do while loop which wont accept invalid inputs..

    these invalid inputs include outside the range of 1-100, negative numbers, and zero, also i would like my loop not to accept floats, negative floats, strings and characters..

    is that possibel? i dont have an idea how to do the latter part, involving not accepting floats ang strings and character..

    here's what i've gotten to so far

    Code:
    /* gets the user's bet */
         do 
         { printf ("Your bet: $ ");
            scanf ("%d", &nBet);
         }
         while (nBet < 1 || nBet > 100);
         printf ("\n");
    thanx!
    Your best bet would be to steer clear of the scanf function altogether. Its usage can cause problems, and since there are other nice solutions which don't involve using scanf, why not try some of them?

    It seems that there are many ways of getting a number from a user, and validating it. Have a look at the relevant FAQ, and you will see what I mean. There are more methods even than what are demonstrated in the FAQ. My personal favourite is to use fgets to read in a line of text, and then convert it to whatever format of number using sscanf.

    Code:
    #include <stdio.h>
    
    static int get_num(int arg_num, int *err_var);
    static void need_num(void);
    
    int main(void)
    {
    
        int bet = 0;
        int getnum_fail = 0;
    
    /* Get the user's bet */
        do {
            printf("Your bet: $ ");
            fflush(stdout);
            bet = get_num(1, &getnum_fail);
            if (getnum_fail == -1) {    /* call to get_num failed */
                need_num();
                getnum_fail = 0;
            }
        } while (bet < 1 || bet > 100);
    
        return 0;
    }
    
    static int get_num(int arg_num, int *err_var)
    {
        enum {
            BUF_SIZE = 100
        };
        int test = 0;
        char buffer[BUF_SIZE] = { (char) 0 };
        int value = 0;
    
        if ((fgets(buffer, BUF_SIZE, stdin)) != NULL) {
            test = sscanf(buffer, "%d", &value);
        }
        if (test != arg_num) {      /* sscanf was a failure */
            *err_var = -1;
        }
        return value;
    }
    
    void need_num(void)
    {
        (void) puts("\nEnter the values in numeric format please.\n");
    }
    Basically I put the call to get_num in a loop and pass it two values. One is the 'magic number' 1 - this number is used in the function to see if sscanf got one conversion. If it did, then we can return that converted number. If it did not, then we set the variable getnum_fail via the pointer we passed in. Then we try again until success.

    I would explain more, but my wife is waiting on me to go out

    For some more very nice examples of getting numbers from the user, check out Dave Sinkula's article on the matter.
    Last edited by kermit; 07-12-2006 at 10:57 AM.

  7. #7
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Code:
    (void) puts("\nEnter the values in numeric format please.\n");
    Why have I seen people needlessly typecasting (always to void) the return type of certain functions lately? Is there some up-and-coming drastic change that I'm not aware of?

    Won't the compiler silently ignore the return value if you don't use it?
    Last edited by itsme86; 07-12-2006 at 10:45 AM.
    If you understand what you're doing, you're not learning anything.

  8. #8
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Its a habit I have gotten into - if you ignore the return value of certain functions, splint will complain. Of course some functions (ie., printf) you can 'safely' ignore the return value - go figure..

  9. #9
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    I didn't realize anyone used lint anymore Thanks for the explanation. I guess I can drop the conspiracy theory.
    If you understand what you're doing, you're not learning anything.

  10. #10
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Well for trivial stuff I don't usually bother, but some of the code I posted above was copied from a program I wrote where I did run it through the checker. Incidentally, my call to fflush would also generate a warning.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 07-05-2010, 10:43 AM
  2. A very long list of questions... maybe to long...
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-16-2007, 05:36 AM
  3. Trivial questions - what to do?
    By Aerie in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 12-26-2004, 09:44 AM
  4. Laptop Comparison Questions
    By KneeGrow in forum Tech Board
    Replies: 15
    Last Post: 01-19-2004, 11:05 PM
  5. questions questions questions.....
    By mfc2themax in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 08-14-2001, 07:22 AM