putting strings together through structures & functions

This is a discussion on putting strings together through structures & functions within the C Programming forums, part of the General Programming Boards category; I can get the names entered in this code but the output is strange characters. Code: #include <stdio.h> #include <stdlib.h> ...

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    15

    Unhappy putting strings together through structures & functions

    I can get the names entered in this code but the output is strange characters.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define NUM_EMPL 5
    
    struct employee
        {
            char name[20]; /*name*/
        };
    
    
    void get_input (struct employee emp[])
    {
        /*Local Variable Declaration */
        
        char first_name[20], last_name[20];
    
        int count; /* Variable used in loop counter */
    
            
         for (count = 0; count < NUM_EMPL; ++count)
          {
            printf("Enter First Name ");
            scanf ("%s", first_name);
            printf("Enter Last Name ");
            scanf ("%s", last_name);
    
          }
                  
            strcpy(emp[count].name, first_name);
            strcat(emp[count].name, " ");
            strcat(emp[count].name, last_name);
          
    
            printf("\n\n\n");
      
    }
    
    
    
    void output_screen (struct employee emp[])
    {
         /*Local Variable Declaration */
    
        int count; /* Variable used in loop counter */
        
        /*print output in table format using arrays */
        
       
    	for (count = 0; count < NUM_EMPL; ++count)
         	{
         	  printf ("Employee name is %s\n", emp[count].name);
         	}
    }
    
    
        
    main()
    {
         struct employee emp[NUM_EMPL];
         
         get_input(emp);
        
          output_screen(emp);
       
    		
    system("PAUSE");	
    return 0;
    }

    my output is similar to this.. not exactly this (didnt know how to capture my output

    Code:
    
    Employee name is
    Employee name is x
    Employee name is  
    Employee name is  
    Employee name is

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,659
    You want your strcpy(),strcat(),strcat() inside the for loop. If the first name and last name can have up to 20 chars, and they are to be concatinated together with a space, then you should have room for 41 characters in the result.
    I'd suggest that you just put a first_name and last_name in struct employee itself.

    gg

  3. #3
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    You've got your strcpy and strcat statements outside of the for loop.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Functions that return structures
    By Burra in forum C Programming
    Replies: 4
    Last Post: 04-25-2008, 04:16 AM
  2. COntrol structures and functions questions
    By angelicscars in forum C Programming
    Replies: 1
    Last Post: 11-21-2005, 10:50 AM
  3. Putting functions in a header file
    By Vertex34 in forum C Programming
    Replies: 6
    Last Post: 09-29-2004, 02:33 PM
  4. Strings - functions
    By Chimpsag in forum C++ Programming
    Replies: 4
    Last Post: 12-29-2002, 08:46 PM
  5. structures and functions
    By akalvarado in forum C Programming
    Replies: 4
    Last Post: 11-24-2002, 10:09 AM

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