Thread: How do I check input for nonnumeric value?

  1. #1
    Registered User
    Join Date
    Feb 2013
    Location
    San Jose, California, United States
    Posts
    22

    How do I check input for nonnumeric value?

    See my code below:

    Code:
    /*
     * ======================================================================
     *
     *       Filename:  ex08.c
     *
     *    Description:  This program asks the user to enter a Fahrenheit
     *                temperature and then converts it into Celsius and
     *                Kelvin temperature.
     *
     *        Version:  1.0
     *        Created:  03/20/2013 04:55:38 PM
     *       Revision:  none
     *       Compiler:  gcc
     *
     *         Author:  Vincent 
     *   Organization:  Vincent`s Workshop
     *
     * ======================================================================
     */
    
    #include <stdio.h>
    
    void Temperatures(double temp_F);
    
    int main(void) {
        double temp;
        
        printf("Enter a temperature in Fahrenheit:\n");
        scanf("%lf", &temp);
    
        while ( temp /* some code here? */ ) {
            Temperatures(temp);
            printf("Enter a temperature in Fahrenheit "
                    "(enter a nonnumeric value to exit):\n");
            scanf("%lf", &temp);
        }
    
        return 0;
    }
    
    void Temperatures(double temp_F) {
        double temp_C;
        double temp_K;
    
        const double C_SCALE = 1.8;
        const double C_ADD = 32.0;
        const double K_ADD = 273.16;
    
        temp_C = (temp_F - C_ADD) / C_SCALE;
        temp_K = temp_C + K_ADD;
    
        printf("%.2f F is %.2f C and %.2f K.\n", temp_F, temp_C, temp_K);
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You can roll your own logic on this, or use isdigit() to test each entered char, to see if it's a digit or not. (include <ctype.h>)

    If you want something done at least once, a do while(test condition), is usually easiest and clearest.

    Use a while loop when you want the test condition to be true, even before entering the loop one time.

  3. #3
    Registered User
    Join Date
    Feb 2013
    Location
    San Jose, California, United States
    Posts
    22
    I followed what you suggested, but right now the program loops once no matter what value I input. I need to make it loop again when the input is a numeric value.

    Here is the modified program:

    Code:
    /*
     * ======================================================================
     *
     *       Filename:  ex08.c
     *
     *    Description:  This program asks the user to enter a Fahrenheit
     *                temperature and then converts it into Celsius and
     *                Kelvin temperature.
     *
     *        Version:  1.0
     *        Created:  03/20/2013 04:55:38 PM
     *       Revision:  none
     *       Compiler:  gcc
     *
     *         Author:  Vincent 
     *   Organization:  Vincent`s Workshop
     *
     * ======================================================================
     */
    
    #include <stdio.h>
    #include <ctype.h>
    
    void Temperatures(double temp_F);
    
    int main(void) {
        double temp;
        
        do {
            printf("Enter a temperature in Fahrenheit "
                    "(enter a nonnumeric value to exit):\n");
            scanf("%lf", &temp);
            Temperatures(temp);
        } while (isdigit(temp));
    
        return 0;
    }
    
    void Temperatures(double temp_F) {
        double temp_C;
        double temp_K;
    
        const double C_SCALE = 1.8;
        const double C_ADD = 32.0;
        const double K_ADD = 273.16;
    
        temp_C = (temp_F - C_ADD) / C_SCALE;
        temp_K = temp_C + K_ADD;
    
        printf("%.2f F is %.2f C and %.2f K.\n", temp_F, temp_C, temp_K);
    }

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Actually I think Adak's advice is pretty awful this time. You should revert to the OP code. To be frank the do-while loop won't help you verify input, so it has no bearing on the answer.

    Since the user can enter non-numeric text, the point of failure is actually scanf(), because that function requests the input. Repeatedly check scanf()'s return value in the following way (citing from this page):
    Upon successful completion, these functions shall return the number of successfully matched and assigned input items; this number can be zero in the event of an early matching failure. If the input ends before the first matching failure or conversion, EOF shall be returned. If a read error occurs, the error indicator for the stream is set, EOF shall be returned
    This way at least you will know when the user entered something numeric because the function would return 1 for assigning to temp.

    Using scanf can be tricky as well.
    Last edited by whiteflags; 03-20-2013 at 09:12 PM.

  5. #5
    Registered User
    Join Date
    Feb 2013
    Location
    San Jose, California, United States
    Posts
    22
    Ok now, I modified again and it worked pretty well. However, if I typed something like "10a" I get something like:

    Enter a temperature in Fahrenheit (enter a nonnumeric value to exit):
    10a
    10.00 F is -12.22 C and 260.94 K.
    Enter a temperature in Fahrenheit (enter a nonnumeric value to exit):

    The program exits to the command line right after it asks me the 2nd question.

    Here is the code:

    Code:
    #include <stdio.h>
     
    void Temperatures(double temp_F);
     
    int main(void) {
        double temp;
         
        printf("Enter a temperature in Fahrenheit:\n");
     
        while ( scanf("%lf", &temp) == 1 ) {
            Temperatures(temp);
            printf("Enter a temperature in Fahrenheit "
                    "(enter a nonnumeric value to exit):\n");
        }
     
        return 0;
    }
     
    void Temperatures(double temp_F) {
        double temp_C;
        double temp_K;
     
        const double C_SCALE = 1.8;
        const double C_ADD = 32.0;
        const double K_ADD = 273.16;
     
        temp_C = (temp_F - C_ADD) / C_SCALE;
        temp_K = temp_C + K_ADD;
     
        printf("%.2f F is %.2f C and %.2f K.\n", temp_F, temp_C, temp_K);
    }
    Is there a way to fix that in a way that the program will exit right away after I enter something like "10a"?

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Not really. If you stick to using scanf you need a more specific format string. I've written about it before, and it's up to you to decide if it's worth the trouble.

  7. #7
    Registered User
    Join Date
    Feb 2013
    Location
    San Jose, California, United States
    Posts
    22
    Ok, I now see why it exits like that. And yeah, it's not worth a trouble fixing this over a homework problem. Thanks much!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Check input!
    By johnybe in forum C Programming
    Replies: 2
    Last Post: 11-09-2009, 11:58 AM
  2. Check for input?
    By raterus in forum C Programming
    Replies: 5
    Last Post: 08-26-2009, 12:23 PM
  3. Input check
    By Extol in forum C++ Programming
    Replies: 2
    Last Post: 02-15-2003, 11:31 AM
  4. check for bad input
    By tcs122499 in forum C++ Programming
    Replies: 9
    Last Post: 01-28-2003, 05:12 PM
  5. how do you check to see if input is an int?
    By pkananen in forum C++ Programming
    Replies: 10
    Last Post: 02-20-2002, 04:57 PM