Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. need code to check if the value is numeric in C

need code to check if the value is numeric in C

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorialquestioncareer
9 Posts 5 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mikerush7
    wrote on last edited by
    #1

    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 ? 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"); if (isdigit(payrate) !=0) return payrate; else printf ("You entered invalid data, please enter payrate \n"); 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);

    L CPalliniC D C 4 Replies Last reply
    0
    • M Mikerush7

      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 ? 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"); if (isdigit(payrate) !=0) return payrate; else printf ("You entered invalid data, please enter payrate \n"); 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);

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      You cannot use a double parameter as input to the isdigit()[^] function. You should read your numbers in as strings and then apply isdigit() to each character, using a loop. Rather than having all your code in the main() function, you may want to think about writing a helper function that will input the string, validate it and return the converted value.

      One of these days I'm going to think of a really clever signature.

      M 1 Reply Last reply
      0
      • L Lost User

        You cannot use a double parameter as input to the isdigit()[^] function. You should read your numbers in as strings and then apply isdigit() to each character, using a loop. Rather than having all your code in the main() function, you may want to think about writing a helper function that will input the string, validate it and return the converted value.

        One of these days I'm going to think of a really clever signature.

        M Offline
        M Offline
        Mikerush7
        wrote on last edited by
        #3

        i dont really know how to do that yet... Ok, i will try.. is there a general format of that function somewhere?

        L 1 Reply Last reply
        0
        • M Mikerush7

          i dont really know how to do that yet... Ok, i will try.. is there a general format of that function somewhere?

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Mikerush7 wrote:

          is there a general format of that function somewhere?

          Assuming you mean isdigit(), I put the link in my previous message.

          One of these days I'm going to think of a really clever signature.

          1 Reply Last reply
          0
          • M Mikerush7

            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 ? 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"); if (isdigit(payrate) !=0) return payrate; else printf ("You entered invalid data, please enter payrate \n"); 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);

            CPalliniC Offline
            CPalliniC Offline
            CPallini
            wrote on last edited by
            #5

            Mikerush7 wrote:

            while ((k = scanf ("%lf%lf", &hours, &payrate))==2)

            If scanf succeeds then you need no other check.

            Mikerush7 wrote:

            should I call the Main function again

            No. You may call scanf (if you are allowed to use it) many times inside your main function.

            Veni, vidi, vici.

            In testa che avete, signor di Ceprano?

            1 Reply Last reply
            0
            • M Mikerush7

              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 ? 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"); if (isdigit(payrate) !=0) return payrate; else printf ("You entered invalid data, please enter payrate \n"); 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);

              D Offline
              D Offline
              David Crow
              wrote on last edited by
              #6

              Mikerush7 wrote:

              while ((k = scanf ("%lf%lf", &hours, &payrate))==2)

              Without a full understanding of assignments and expressions, mixing the two in one statement is highly discouraged.

              Mikerush7 wrote:

              return hours;

              While I'm not sure who you'd be returning hours worked to or why, the compiler should have at least warned you about loss of data.

              "One man's wage rise is another man's price increase." - Harold Wilson

              "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

              "Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous

              L 1 Reply Last reply
              0
              • D David Crow

                Mikerush7 wrote:

                while ((k = scanf ("%lf%lf", &hours, &payrate))==2)

                Without a full understanding of assignments and expressions, mixing the two in one statement is highly discouraged.

                Mikerush7 wrote:

                return hours;

                While I'm not sure who you'd be returning hours worked to or why, the compiler should have at least warned you about loss of data.

                "One man's wage rise is another man's price increase." - Harold Wilson

                "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                "Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                DavidCrow wrote:

                the compiler should have at least warned you

                Given the number of syntax errors elsewhere I don't think a compiler has ever seen this sample.

                One of these days I'm going to think of a really clever signature.

                M 1 Reply Last reply
                0
                • L Lost User

                  DavidCrow wrote:

                  the compiler should have at least warned you

                  Given the number of syntax errors elsewhere I don't think a compiler has ever seen this sample.

                  One of these days I'm going to think of a really clever signature.

                  M Offline
                  M Offline
                  Mikerush7
                  wrote on last edited by
                  #8

                  thank you everybody for help. I will keep working on it. my teacher is not helping, so I am on my own..

                  1 Reply Last reply
                  0
                  • M Mikerush7

                    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 ? 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"); if (isdigit(payrate) !=0) return payrate; else printf ("You entered invalid data, please enter payrate \n"); 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);

                    C Offline
                    C Offline
                    C Mahesh
                    wrote on last edited by
                    #9

                    Build your logic based on the following example. int main( ) { char n ; int no = 0 ; printf("Enter a no " ); while( (n = (char)getche( )) != '\r') //Check for enter key { if( n >= '0' && n <= '9' ) { n = n - 48 ; no = (no * 10) + n ; } else{ printf("\nInvalid. "); } } printf("\n %d " , no ) ; return 0 ; }

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups