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

Recommended Answers

All 5 Replies

i changed it to what the link says

fflush(stdin);
   fputs("\n   Please enter your First Name: ", stdout);
   fflush(stdout);
   if (fgets(first_name_p, sizeof first_name_p, stdin) != NULL)
   {
      char *newline = strchr(first_name_p, '\n');
	  if (newline != NULL)
	  {
		  *newline = '\0';
	  }
	  printf("first_name_p = \"%s\"\n", 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);

and now let say I input "Michael" for first name, the program prints:

first_name_p = "Mic"
Your first name is too long, use shorter name
....

which means that for some reason it gets only 3 characters. What's even more crazy, is that it thinks that those 3 characters are bogger than MAXFL (which is 10)???

what's up with that?

Member Avatar for iamthwee

Do you know what strlen() is?

I read about it in some string tutorial but no example was shown so if you could provide example that be great.

Member Avatar for iamthwee
#include <stdio.h>
#include <string.h>
 
int main()
{
   char text[100];
   fputs("enter some text ugly: ", stdout);
   fflush(stdout);
   if ( fgets(text, sizeof text, stdin) != NULL )
   {
      char *newline = strchr(text, '\n'); /* search for newline character */
      if ( newline != NULL )
      {
         *newline = '\0'; /* overwrite trailing newline */
      }
      printf("text = \"%s\"\n", text);
      
      int size = strlen(text);
      printf("It was %d letters",size);
   }
   return 0;
}
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.