Thread: need code to check if the value is numeric in C

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    3

    need code to check if the value is numeric in C

    I am student, taking C programming now.
    I have assignment to write function to check if inputed data is numbers. if not - it should write that, and ask to enter number again.
    I wrote code, to write error message, but dont know how to make program to work on next input.. should I call the Main function again ?

    Code:
    int main()
    {
    
       double hours;               //hours worked in a week
       double payrate;            // hourly salary
       double grosspay;            //total weekly payment
       int k;                           //number of values read
    
    printf ("Enter number of hours, and hourly pay (or press 0 to exit) \n");
    
          while ((k = scanf ("%lf%lf", &hours, &payrate))==2)
          {
    
          if (isdigit(hours) !=0)
                return hours;
                else 
                printf ("You entered invalid data, please enter number of hours \n");
    scanf ("%lf, &hours)
    
          if (isdigit(payrate) !=0)
                return payrate;
                else 
                printf ("You entered invalid data, please enter payrate \n");
    scanf ("%lf, &payrate)   
    
          if (hours==0 || payrate==0) 
                   {
                   printf ("Good Bye. \n\n");
                   system("PAUSE");
                   return 0;
                   } 
    
       double grosspay;
       grosspay = calculateGrossPay(hours, payrate);
    
       printf ("Hours worked: %.2f\n", hours);
       printf ("Hourly   Rate: %.2f\n\n", payrate);
       printf ("Your weekly payment is: $ %.2f \n\n\n", grosspay);
    
    
    


    I checked the page "How do i get a number from the user", but still did not understand how to fix it.. Any advice would be appreciated.
    Thank you !

  2. #2
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by mikerush7 View Post
    I checked the page "How do i get a number from the user", but still did not understand how to fix it
    What's your problem with this page?

    Basically you have to decide if you want to use scanf() or write your own validation function. From your assignment description it seems you have to write your own, right?
    Do you know already how strings work in C and how to get them from the user?

    Bye, Andreas

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    1. you do not call main - if you need to do it - move contents of the main into another function and then call it in the loop
    2. your usage of isdigit is wrong - you do not appy it on the double. if you have a character you can use this function to check if the character is representing a digit
    3. you logic is at fault - if the input does not contains 2 double values - scanf will fail - so your code inside the loop will not work in this case. you should think another way to implement the requested task
    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

  4. #4
    Registered User
    Join Date
    Oct 2012
    Posts
    3
    Yes, I need to write my own function. reading in the book and on internet, I found out that I need to create function that will check the number that I enter - as characters, one by one, and I need to create a loop, so it will check each character after another. I understand the idea, but I dont know, how to write the actual code..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. alpha-numeric to numeric program
    By johnhuge in forum C Programming
    Replies: 2
    Last Post: 03-24-2012, 12:08 AM
  2. Help! Check my code over.
    By omGeeK in forum C Programming
    Replies: 6
    Last Post: 11-04-2010, 11:34 PM
  3. Code Check
    By Dae in forum C++ Programming
    Replies: 12
    Last Post: 01-08-2009, 07:01 PM
  4. check command line argument for numeric value
    By ephemeralz in forum C Programming
    Replies: 4
    Last Post: 03-17-2004, 05:22 PM
  5. check the code
    By pasha in forum Windows Programming
    Replies: 2
    Last Post: 01-06-2003, 02:01 PM