troubles with gets() function @ void add_employee()

This is a discussion on troubles with gets() function @ void add_employee() within the C Programming forums, part of the General Programming Boards category; hello,i'm having troubles with gets() function @ void add_employee() nothing works i can't accept data, please help me. Code: #include ...

  1. #1
    Registered User
    Join Date
    Feb 2012
    Location
    Trinidad & Tobago
    Posts
    43

    troubles with gets() function @ void add_employee()

    hello,i'm having troubles with gets() function @ void add_employee()
    nothing works i can't accept data, please help me.
    Code:
    #include <stdio.h>
    #include <string.h> 
    
    
    struct employee
    {
           int employee_ID;
           char employee_name[50];
           int employee_age;
           char employee_DateOfBirth[10];
           char employee_address[70];
           char employee_TelNo[20];
           float emplayee_salary;
           
    }employee_array[50];  // structure variable 
    
    
    void print_line();     // function prototypes
    void add_employee();
    void edit_employee();
    void delete_employee();
    void search_employee();
    void print_employee();
    
    
    int main()
    {
        int option;
       //clrscr();
       print_line();
       printf("This program provides the functionality to \n");
       printf("interface with an employee information system.");
       print_line();
       printf("Enter  [1]   -   to enter employee profile \n");
       printf("Enter  [2]   -   to edit employee \n");
       printf("Enter  [3]   -   to delete employee \n");
       printf("Enter  [4]   -   to search for employee \n");
       printf("Enter  [5]   -   to print employee information \n");
       printf("Enter  [0]   -   to exit\n\n");
       printf("Please select an option:\t");
       scanf("%d",&option);
       getchar();
       while(option != 0) 
       {
               switch(option)
                       {
                           case 1: add_employee();
                           break;
                           case 2: edit_employee();
                           break;
                           case 3: delete_employee();
                           break;
                           case 4: search_employee();
                           break;
                           case 5: print_employee();
                           break;
                           default:
                           printf("\n\nIncorrect Entry!,Please press enter and Try again!\n\n");
                           getch();
                       }
          printf("\n\n");                
          print_line();
          printf("This program provides the functionality to \n");
          printf("interface with an employee information system.");
          print_line();
          printf("Enter [ 1 ]   -   to enter employee profile \n");
          printf("Enter [ 2 ]   -   to edit employee \n");
          printf("Enter [ 3 ]   -   to delete employee \n");
          printf("Enter [ 4 ]   -   to search for employee \n");
          printf("Enter [ 5 ]   -   to print employee information \n");
          printf("Enter [ 0 ]   -   to exit\n\n");
          printf("Please select an option:\t");
          scanf("%d",&option);
       }
       getch();
    }
      
      
       
    void print_line()
    {
      int i;
      printf("\n");
        for(i=0;i<48;i++)
         {
           printf("-");
         }
      printf("\n");
    }
    
    
    void add_employee() // decleration of add employee function
    {    
         int i;
          printf("\nEnter employee ID or 0 to exit:\t");
          scanf("%d",&employee_array[i].employee_ID);
          while(employee_array[i].employee_ID !=0){
                 printf("Enter emplayee name:\t");
                 gets(employee_array[i].employee_name);
                 printf("\nEnter employee age");
                 scanf("%d",&employee_array[i].employee_age);
                 printf("\nEnter employee Date-Of-Birth:\t"); 
                 gets(employee_array[i].employee_DateOfBirth);
                 printf("\nEnter employee address:\t");
                 gets(employee_array[i].employee_address);
                 printf("\nEnter employee telephone number");
                 gets(employee_array[i].employee_TelNo);
                 printf("\nEnter employee salary");
                 scanf("$%f",&employee_array[i].emplayee_salary);
                 printf("\n\nEnter employee ID or 0 to exit:\t");
                 scanf("%d",&employee_array[i].employee_ID);
                 fflush(stdin);
                 i++;
         }
    } 
    void edit_employee()
    {
         
         }
    void delete_employee()
    {
         
         }
    void search_employee()
    {
         
         }
    void print_employee()
    {
         
         }

  2. #2
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,648

  3. #3
    Registered User
    Join Date
    Feb 2012
    Location
    Trinidad & Tobago
    Posts
    43
    well what can i use instead ? all i know at present is scanf and gets

  4. #4
    Registered User
    Join Date
    Feb 2012
    Location
    Trinidad & Tobago
    Posts
    43
    Thanks for the link btw, very helpful

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    535
    Quote Originally Posted by Shinzu911 View Post
    well what can i use instead ? all i know at present is scanf and gets
    Use fgets. It's similar to gets but does size checking. For example:

    Code:
    fgets(employee_array[i].employee_DateOfBirth, sizeof(employee_array[i].employee_DateOfBirth), stdin);
    Couple of comments:
    In add_employee() you use uninitialised variable i as an index. i could be absolutely anything.

    You're probably getting hit by stray newline characters. So you type something and hit enter, what happens to the newline char? fgets (and I think gets) read the newline character from stdin and store it at the end of the string.
    Scanf however does not. So in your program you'll read employee ID, then the next (f)gets will just read the pending newline character and not get any input from the user.
    You could follow scanfs with a getchar() -- or use the %*c =, or wrap scanf in a function to deal with this for you. Plenty of options.
    It looks pretty fragile at the moment -- having a function of scanfs without checks for success and retries will go horribly wrong on bad input.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. malloc () function troubles.
    By pvrgallardo in forum C Programming
    Replies: 6
    Last Post: 04-17-2012, 05:44 AM
  2. difference between void and static void function
    By mahaju in forum C++ Programming
    Replies: 7
    Last Post: 12-27-2011, 03:02 AM
  3. Function and pointer troubles
    By ulillillia in forum C Programming
    Replies: 7
    Last Post: 04-25-2009, 04:25 PM
  4. function troubles again
    By wwwildbill in forum C Programming
    Replies: 3
    Last Post: 02-22-2009, 03:27 PM
  5. Passing a variable in void to another void function
    By stevedawg85 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 06:17 PM

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