Thread: problem with validating data inside function

  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
    28,413
    What exactly is the problem?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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,218
    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
    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, 02:53 AM
  2. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  3. Replies: 5
    Last Post: 02-08-2003, 07: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