I have problems validated user's input inside the function, I know validating strings can be tricky( I have included > MAXFL just to show you want I would like it to do), but the code below even fails to validate the hours, pay_rate, and deferred which are floats. What is wrong? does it have to do with the pointers? if so, how do I go around it?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void Input_Employee(char*, char*, char*, float*, float*, float*); //prototype
int main(void)
{
#define MAXFL 10 /*Maximum # of char's for first, last name*/
#define MAXFULL 20 /*Maximum # of char's for full name*/
char first_name[MAXFL], last_name[MAXFL], full_name[MAXFULL];
float hours, pay_rate, deferred;
Input_Employee(full_name, last_name, first_name, &hours, &pay_rate,
&deferred); //function call
printf("\n\n Press any key to terminate . . . \n");
getchar();
return 0;
}
/************************** Function Definition #x *****************************
* Function: Input_Employee( ) *
* Description: This function gets and validates Employee's input. *
* Arguments: *full_name_p, *last_name_p, *first_name_p, *hours_p, *
* *pay_rate_p, *deferred_p *
* Return value: NOTHING *
*******************************************************************************/
void Input_Employee(char *full_name_p, char *last_name_p, char *first_name_p,
float *hours_p, float *pay_rate_p, float *deferred_p)
{
printf("\n Please enter your First Name: ");
scanf("%s", first_name_p);
while (first_name_p < MAXFL)
{
printf(" Your first name is too long, use shorter name");
printf("\n Please enter your First Name: ");
scanf("%s", first_name_p);
}
printf(" Please enter your Last Name: ");
scanf("%s", last_name_p);
while (last_name_p > MAXFL)
{
printf(" Your last name is too long, use shorter name");
printf("\n Please enter your Last Name: ");
scanf("%s", last_name_p);
}
strcpy(full_name_p, last_name_p);
strcat(full_name_p, ", ");
strcat(full_name_p, first_name_p);
printf(" Please enter your hourly pay rate: ");
scanf("%f", pay_rate_p);
while (pay_rate_p < 0)
{
printf(" Invalid data, try again");
printf(" Please enter your hourly pay rate: ");
scanf("%f", pay_rate_p);
}
printf(" Please enter total hours worked for this pay period: ");
scanf("%f", hours_p);
while (hours_p < 0)
{
printf(" Invalid data, try again");
printf(" Please enter total hours worked for this pay period: ");
scanf("%f", hours_p);
}
printf(" Please enter the amount of your Deferred Earnings: ");
scanf("%f", deferred_p);
while (deferred_p < 0)
{
printf(" Invalid data, try again");
printf(" Please enter the amount of your Deferred Earnings: ");
scanf("%f", deferred_p);
}
}