Thread: Help with isdigit?

  1. #1
    Registered User
    Join Date
    Jun 2015
    Posts
    5

    Help with isdigit?

    So, I'm creating a program that calculates the area of a triangle, and I need it to tell the user if he typed a letter or a negative number, in order, I created the code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <ctype.h>
        int main () {
            float a, b, c;
            float s=0, ar1=0, ar2=0;
            printf("Inform the value of side A.");
            fflush(stdin);
            scanf("%f",&a);
            while(a<=0||isdigit((int)a)){
                printf("Invalid value.");
                fflush(stdin);
                scanf("%f",&a);
            }printf("Inform the value of side B.");
            fflush(stdin);
            scanf("%f",&b);
            while(b<=0||isdigit((int)a)){
                printf("Invalid value.");
                fflush(stdin);
                scanf("%f",&b);
            }printf("Inform the value of side C.");
            fflush(stdin);
            scanf("%f",&c);
            while(c<=0||isdigit((int)a)){
                printf("Invalid value.");
                fflush(stdin);
                scanf("%f",&c);}
                 s=((a+b+c)/2);
                ar1=(s*(s-a)*(s-b)*(s-c));
                ar2=pow(ar1,0.5);
           printf("The semiperimeter is %f",s);
           printf("The area of the triangle is%f",ar2);
           system ("pause");
           return 1;
    }
    But, when I compile/run it, and type "x", or "blabla" when I was supposed to type a number, nothing happens, and the program doesn't warn me, what should I do?

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    see my answer in the following thread

    Code not working properly.

    Just in your case it will be strtod function instead of strtol


    also read FAQ about fflush(stdin)
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Jun 2015
    Posts
    5
    Hmm, I'm kind of beginner in programming, I don't know how to use "strtod/strtol" =(

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    int main()
    {
        const char * src = "123junk\n";
        char* pEnd;
        long l = strtol(src,&pEnd,10);
    
        if(pEnd == src || (*pEnd != '\n' && *pEnd != 0))
        {
            printf("Following part of the input is not a number: %s\n", pEnd);
        }
        else
        {
            printf("You have entered: %d\n", (int)l);
        }
    
    
        return 0;
    }
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User
    Join Date
    Jun 2015
    Posts
    5
    Hmm, I get it, but how to use that + isdigit in my code?
    I tried several ways and searched for lots of functions in boards, but I really cant make this code work properly =(

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You don't need isdigit if you're going to use strtol fully as demonstrated.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. isdigit()
    By a.mlw.walker in forum C Programming
    Replies: 10
    Last Post: 04-14-2009, 11:43 AM
  2. using isdigit()
    By Beowolf in forum C Programming
    Replies: 7
    Last Post: 09-10-2007, 04:50 PM
  3. isdigit
    By bazzano in forum C Programming
    Replies: 3
    Last Post: 04-02-2007, 03:14 PM
  4. isdigit()
    By kermit in forum C Programming
    Replies: 3
    Last Post: 03-19-2004, 09:59 PM
  5. isdigit
    By AmazingRando in forum C Programming
    Replies: 3
    Last Post: 09-21-2003, 03:14 PM