Hello. I am writing a program that computes net pay by the user entering in hours and payrate. There can only be one decimal, two numbers after the decimal, and no letters. When payrate is entered wrong, it displays the correct error message. For hours, it doesn't even though it contains the same code except for a few variable differences. Please let me know if I am missing something. I can post the code to NetPayCalculator if needed. Thanks.

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

int main(int argc, char *argv[])
{
  void NetPayCalculator(double hours, double payrate);

  double hours;
  double payrate;

  int dec_counter = 0;
  int after_dec = 0;
  char h[20]= {'\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
              '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0'};
    /*setting to '\0' to keep bad data out of arrays. Zeroing out didn't achieve correct results either.*/
  char p[20] = {'\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
              '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0'};

  int a = 0;
  int b = 0;

  if (argc != 3)   /*checks to make sure two arguments passed, along with function*/
  {
           printf("Information entered incorrectly. Please enter the program name, hours, and payrate with only spaces and no punctuation.\n\n");
           exit ( 0 );
  }

  strcpy (h, argv[1]); /*copy hours to new string*/
  strcpy (p, argv[2]); /*copy payrate to new string*/

 for(a = 0; h[a] != '\0'; a++)  /*hour error check*/
{
      if (isalpha(h[a]) != 0)
      {
                        printf("Information contained letters. Please enter a number.\n");
                        exit ( 0 );
      }

      if (ispunct(h[a]) != 0)
      {
                        dec_counter++;
      }
    if(dec_counter >= 2)
        {
                printf("Information containted too many decimals. Please enter with only one decimal.\n");
                     exit ( 0 );
         }
      if (dec_counter == 1)
      {
                      if(isdigit(h[a]) != 0)
                      {
                      after_dec++;
                      }
      }
      if (after_dec >= 3)
      {
                printf("Information contained too many numbers after the decimal. Please enter with only two numbers maximum after decimal.\n");
                exit ( 0 );
      }
}


hours = atof( h ); /*convert hours to floating point number*/


 for(b = 0; p[b] != '\0'; b++) /*payrate error check*/
{
      if (isalpha(p[b]) != 0)
      {
                        printf("Information contained letters. Please enter a number.\n");
                        exit ( 0 );
      }

      if (ispunct(p[b]) != 0)
      {
                        dec_counter++;
      }
    if(dec_counter >= 2)
        {
                printf("Information containted too many decimals. Please enter with only one decimal.\n");
                     exit ( 0 );
         }
      if (dec_counter == 1)
      {
                      if(isdigit(p[b]) != 0)
                      {
                      after_dec++;
                      }
      }
      if (after_dec >= 3)
      {
                printf("Information contained too many numbers after the decimal. Please enter with only two numbers maximum after decimal.\n");
                exit ( 0 );
      }
}

payrate = atof( p ); /*convert payrate to floating point number*/

  NetPayCalculator(hours, payrate);  /*function to compute net pay*/

  return EXIT_SUCCESS;
}

Do you have a specific question or do you expect us to run and debug your code?

Wouldn't something like this be easier?

#include <stdio.h>
#include <stdbool.h>
#include <string.h>

bool ParsePayRate(const char *text, int* dollars, int* cents)
{
    unsigned int consumed = 0;
    return ((sscanf(text, "%d.%2d%n", dollars, cents, &consumed) == 2) && (consumed == strlen(text)));
}

bool ParseHours(const char *text, int* hours)
{
    unsigned int consumed = 0;
    return ((sscanf(text, "%d%n", hours, &consumed)) && (consumed == strlen(text)));
}

int main(int argc, char *argv[])
{
    int dollars, cents, hours;

    if (argc != 3)
    {
        printf("Usage: %s <payrate> <hours>\n", argv[0]);
    }
    else
    {
        // Parse the pay rate.
        if (ParsePayRate(argv[1], &dollars, &cents))
        {
            printf("Pay rate successfully parsed. Dollars: %d, cents: %d.\n", dollars, cents);
        }
        else
        {
            printf("Invalid pay rate.\n");
        }

        // Parse the hours.
        if (ParseHours(argv[2], &hours))
        {
            printf("Hours successfully parsed. Hours: %d.\n", hours);
        }
        else
        {
            printf("Invalid hours.\n");
        }
    }

    return 0;
}

I guess my question would be why does payrate go through its error check and why hours doesn't? Is there something in my code that jumps out and says that having hours run through it doesn't work correctly? I have a similar program that prompts the user for hours and payrate and that works fine(but doesnt use command line arguments) Its just a simple net pay calculator, so you could slap a netpay = hours * payrate to test it.

I found the error. The program I had before I had separate error checking programs and used dec_counter and after_dec in each program and didn't occur to me until a few mins ago that they never reset. Thanks for anyone who took time to look at this.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.