problem with validating data inside function

This is a discussion on problem with validating data inside function within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <stdlib.h> #include <string.h> void Input_Employee(char*, char*, char*, float*, float*, float*); //prototype int main(void) { #define MAXFL ...

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    17

    problem with validating data inside function

    Code:
    #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);
       }
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,452
    What exactly is the problem?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2007
    Posts
    17
    Quote Originally Posted by laserlight View Post
    What exactly is the problem?
    well... I'm not sure how to validate first and last name, and validating hours, pay_rate, and deferred fails (no matter what I input, it accepts it)

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    Try dereferencing your pointers in your while() conditions.
    If you understand what you're doing, you're not learning anything.

  5. #5
    Registered User
    Join Date
    Jun 2007
    Posts
    17
    Quote Originally Posted by itsme86 View Post
    Try dereferencing your pointers in your while() conditions.
    it worked!!! I swear I tried that yesterday and it was crashing in the middle...

    anyways, now if I could only validate those damn strings... anyone?

  6. #6
    Registered User
    Join Date
    Jun 2007
    Posts
    17
    Erased
    Last edited by DeathEvil; 07-10-2007 at 12:13 PM.

  7. #7
    Registered User
    Join Date
    Jun 2007
    Posts
    17
    Update, as it turns out the validation of hours, pay_rate, and deferred is working now but the first and last name does not. What happens is, whatever I input it says it's too long (even when I type only one character)

    any help?

  8. #8
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
    while (last_name_p > MAXFL) {
    this compares a pointer and an int. You would have to use
    Code:
    while (strlen(last_name_p) > MAXFL) {
    but then if the user would have entered a name that doesn't fit into the buffer your program would already have crashed.
    you could use fgets() for the string input or
    something like this
    Code:
    scanf("%9s", last_name_p);
    Kurt

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  2. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 09:18 AM
  3. Replies: 5
    Last Post: 02-08-2003, 06:42 PM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21