function to remove newline character (fgets)

This is a discussion on function to remove newline character (fgets) within the C Programming forums, part of the General Programming Boards category; i edited my program using the fgets function, and read about it on how to remove the new line character ...

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

    function to remove newline character (fgets)

    i edited my program using the fgets function, and read about it on how to remove the new line character , but since my program deals with and array of structures i'm really confused as to how i go about making a function for this. e,g
    Code:
    void remove_newline(what should be my arguments)
    {
         int x=0;     
         for(x=0; x<what ;x++){
               if ( str[i] == '\n' ){
                  str[i] = '\0';
               }           
         }
    }
    what should i put to be less that x as it's an array of structures and the size of each structure member is different
    here is my code:
    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();
    void remove_newline();
    
    
    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=0;
         int employ_id;
         
         printf("\nEnter employee ID or 0 to exit:\t");
         scanf("%d",&employ_id);
         while(employ_id!=0){
                             
                 employee_array[i].employee_ID;            
                 printf("Enter emplayee name:\t");
                 fflush(stdin);
                 fgets(employee_array[i].employee_DateOfBirth,sizeof(employee_array[i].employee_DateOfBirth), stdin);
                 remove_newline();
                 printf("\nEnter employee age");
                 scanf(employee_array[i].employee_age);
                 fflush(stdin);
                 printf("\nEnter employee Date-Of-Birth:\t"); 
                 fgets(employee_array[i].employee_DateOfBirth,sizeof(employee_array[i].employee_DateOfBirth), stdin);
                 remove_newline();
                 printf("\nEnter employee address:\t");
                 fgets(employee_array[i].employee_address,sizeof(employee_array[i].employee_address), stdin);
                 remove_newline();
                 printf("\nEnter employee telephone number");
                 fgets(employee_array[i].employee_TelNo,sizeof(employee_array[i].employee_TelNo), stdin);
                 remove_newline();
                 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);
                 
                 i++;
         }
    } 
    void edit_employee()
    { 
        
    }
         
    void delete_employee()
    {
         
         }
    void search_employee()
    {
         
         }
    void print_employee()
    {
         
         }
    void remove_newline(){
         int x=0;
         
         for(x=0; x< ;x++){
               if ( str[i] == '\n' ){
                    str[i] = '\0';
               }           
         }
         
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,786
    The parameter should be a char*. Also, I suggest using strchr to find the newline character. If it exists, you then set it to be a null character.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Remove ASCII and Newline Characters
    By krouthk in forum C Programming
    Replies: 3
    Last Post: 01-12-2010, 11:15 AM
  2. Remove newline char at the end
    By Ducky in forum C++ Programming
    Replies: 9
    Last Post: 12-23-2009, 01:29 PM
  3. how to remove newline from a string
    By rupurt in forum C Programming
    Replies: 8
    Last Post: 10-01-2005, 06:01 PM
  4. Remove the newline from buffer
    By mrbump2004 in forum C Programming
    Replies: 5
    Last Post: 12-06-2004, 04:29 PM
  5. fgets moving on to a newline
    By GanglyLamb in forum C Programming
    Replies: 3
    Last Post: 11-05-2002, 01:57 PM

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