error: cannot convert from 'char [13]' to 'char [20]'

This is a discussion on error: cannot convert from 'char [13]' to 'char [20]' within the C Programming forums, part of the General Programming Boards category; cannot convert from 'char [13]' to 'char [20]' how do i fix this? my struct emp is: struct employee { ...

  1. #1
    Unregistered
    Guest

    Question error: cannot convert from 'char [13]' to 'char [20]'

    cannot convert from 'char [13]' to 'char [20]'

    how do i fix this? my struct emp is:
    struct employee
    {
    char name[20];
    int id_number;
    float wage;
    float hours;
    float overtime;
    float gross;
    };

    and my data is:

    emp[0].name ="Connie Cobol";
    emp[1].name ="Mary Apl";
    emp[2].name ="Frank Fortran";
    emp[3].name ="Jeff Ada";
    emp[4].name ="Anton Pascal";

    I get the error pointing to all 5 of these lines...

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Try strcpy().

    strcpy(emp[0].name, "Connie Cobol");
    strcpy(emp[1].name, "Mary Apl)";
    strcpy(emp[2].name, "Frank Fortran)";
    strcpy(emp[3].name, "Jeff Ada)";
    strcpy(emp[4].name, "Anton Pascal)";

  3. #3
    Unregistered
    Guest

    strcpy worked. one more question

    when printing the data I can't get it to lone up properly because of the different lengths of the names. How can i make it display the names so that they will always be 20 spaces (name + spaces)

    My printf looks like this:

    printf ("%s %06i %5.2f %5.1f %5.1f %5.2f\n", emp[i].name, emp[i].id_number, emp[i].wage, emp[i].hours, emp[i].overtime, emp[i].gross);

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    You could try this:
    Code:
    strcpy(emp[1].name, "Mary Apl)";
    TempSize=strlen(emp[1].name);
    emp[1].name[TempSize]=' '; //Removes NULL terminating character
    emp[1].name[19]='\0'; //Adds NULL terminating character
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Use "%20s" to make all output of names a fixed 20 characters in length.

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Unregistered
    Guest

    almost there......

    when i uaws the %20s it does fix them to 20 but it appends the extra spaces to the beginning. how do i make it so it appends the extra spaces to the end?

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    How about?

    %-20s

  8. #8
    Unregistered
    Guest

    Cool that worked :)

    Thanks everyone. programs working perfect now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 10-29-2006, 04:04 AM
  2. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  3. Need help understanding info in a header file
    By hicpics in forum C Programming
    Replies: 8
    Last Post: 12-02-2005, 11:36 AM
  4. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 10:28 PM
  5. convert long to pointer to char array
    By gazmack in forum C++ Programming
    Replies: 5
    Last Post: 09-26-2003, 11:33 AM

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