How do you assign Full names to a array string

This is a discussion on How do you assign Full names to a array string within the C Programming forums, part of the General Programming Boards category; Like "Rob Willams" this is what I have char Employee1 [60];...

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    20

    How do you assign Full names to a array string

    Like "Rob Willams"

    this is what I have


    char Employee1 [60];
    Last edited by Demipimp; 12-05-2008 at 12:36 AM. Reason: I messed up

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Several ways. Through an initialization statement, or via a built-in function like strcpy().
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  3. #3
    Registered User
    Join Date
    Dec 2008
    Posts
    20

    Like

    By using scanf I'm putting in information for a pay roll

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    I'm not going there. I smell a snowball.

    Post your complete question. I'm not going to be sucked into a 20-questions one-at-a-time thread.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  5. #5
    Registered User
    Join Date
    Dec 2008
    Posts
    20
    your right sorry repost an a decent question now

  6. #6
    POeT GuY Matus's Avatar
    Join Date
    Feb 2008
    Location
    Bz
    Posts
    235
    fgets(employee1,sizeof(employee1),stdin);
    employee1[strlen(employee1)-1]='\0'; //To trim off newline character

    i dunno if that answers your question
    PoEms R InsPiRatiOns of LIfE ExpErienCes!!


  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >//To trim off newline character
    Or whatever else is there if fgets terminates by filling up the buffer instead of finding a newline. This is a subtle bug. You need to guarantee that you're trimming the right character, or do nothing. For example using strlen:
    Code:
    size_t len = strlen ( Employee1 );
    
    if ( len > 0 && Employee1[len - 1] == '\n' )
      Employee1[len - 1] = '\0';
    The most common method is with strchr:
    Code:
    char *newline = strchr ( Employee1, '\n' );
    
    if ( newline != NULL )
      *newline = '\0';
    Some more exotic ones you'll see are strtok and strcspn:
    Code:
    strtok ( Employee1, "\n" );
    Code:
    Employee1[strcspn ( Employee1, "\n")] = '\0';
    My best code is written with the delete key.

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Demipimp View Post
    By using scanf I'm putting in information for a pay roll
    You are probably using %s which ends at a space, so you just get "Robin". Use %[A-za-z ] for your conversion instead, and notice there is a space between the brackets.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #9
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    The problem I have with MK27's answer is that if most software used this method I would never receive mail for "Matt7ew" or any other fun alphanumeric names that I commonly get. And I am sorry, sir, but I cannot imagine a world without such things.

  10. #10
    POeT GuY Matus's Avatar
    Join Date
    Feb 2008
    Location
    Bz
    Posts
    235
    Quote Originally Posted by Prelude View Post
    >//To trim off newline character
    Or whatever else is there if fgets terminates by filling up the buffer instead of finding a newline. This is a subtle bug. You need to guarantee that you're trimming the right character, or do nothing. For example using strlen:

    [/code]
    Hmm ok understood
    PoEms R InsPiRatiOns of LIfE ExpErienCes!!


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 07:52 PM
  2. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 10:19 AM
  3. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  4. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 10:48 AM

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