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;
}