Thread: Name Problem: First, Middle, Last

  1. #16
    Bit Fiddler
    Join Date
    Sep 2009
    Posts
    79
    The scanf directly from stdin requires you to type all three names. Put the user input into a buffer instead and scan that.

    Code:
        char inputBuffer[77];
        char First[25];
        char Middle[25];
        char Last[25];
        
        printf("Please Enter Your Name: ");
        fgets(inputBuffer, 75, stdin);
        sscanf(inputBuffer, "%25s %25s %25s", First, Middle, Last);
        [...]

  2. #17
    Registered User
    Join Date
    Jan 2011
    Posts
    3

    Welcome First Middle Last

    Code:
    #include <stdio.h>
    int main (void)
    {
         printf("Please enter your first name   ");
         gets(First);
          printf("Please enter your middle name   ");
         gets(Middle);
          printf("Please enter your last name   ");
         gets(Last);
    
          printf("\nWelcome %s %s %s",First,Middle,Last);
    
         return(0);
    }
    /*
    You could also do something like:

    #include <stdio.h>
    int main (void)
    {
    printf("Please enter your middle name ");
    gets(Middle);
    printf("Please enter your last name ");
    gets(Last);
    printf("Please enter your first name ");
    gets(First);
    printf("\nWelcome %s %s %s",First,Middle,Last);

    return(0);
    }
    */

  3. #18
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    Quote Originally Posted by Seasick View Post
    Code:
    #include <stdio.h>
    int main (void)
    {
         printf("Please enter your first name   ");
         gets(First);
          printf("Please enter your middle name   ");
         gets(Middle);
          printf("Please enter your last name   ");
         gets(Last);
    
          printf("\nWelcome %s %s %s",First,Middle,Last);
    
         return(0);
    }
    /*
    You could also do something like:

    #include <stdio.h>
    int main (void)
    {
    printf("Please enter your middle name ");
    gets(Middle);
    printf("Please enter your last name ");
    gets(Last);
    printf("Please enter your first name ");
    gets(First);
    printf("\nWelcome %s %s %s",First,Middle,Last);

    return(0);
    }
    */
    yes he can...but that is bad advice...
    gets is prone to buffer overflow, safe to use scanf()...safer to use fgets()
    You ended that sentence with a preposition...Bastard!

  4. #19
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    @Welcome to the forum, Seasick - and believe me, I have known THAT feeling several times.

  5. #20
    Registered User
    Join Date
    Jan 2011
    Posts
    3
    @Adak ty and@Eman true, however an overflow if name has more than 25 characters. And, I believe, there's escape routines that would prevent Unhandled Exception like a Processor Fault. Just my 2 cents worth.

  6. #21
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    I don't know of any "escape routines" to prevent any kind of exception. Some languages have some protection against buffer overflows, but C is not one of them. Eman is right, gets is very dangerous. In fact, it's so dangerous that the documentation tells you to never use it, and that my compiler (GCC) by default warns you if you use it. The "overflow if name has more than 25 characters" is a bogus argument too, since some people have names that long (or longer), and you're assuming the user is going to behave. scanf with the field width specifier is safer though fgets is the best bet.

  7. #22
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Seasick View Post
    @Adak ty and@Eman true, however an overflow if name has more than 25 characters. And, I believe, there's escape routines that would prevent Unhandled Exception like a Processor Fault. Just my 2 cents worth.
    How about using...
    Code:
    int main(void)
      { char First[25];
         // etc.
    
        scanf("%25s", First);
        // etc.
     
       return 0; }

  8. #23
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    That was my initial thought too. I was thinking a scanf with a "%25s" was no different than a fgets of appropriate size, but then I realized there are people with spaces in one part of their name, such as Charles de Gaulle, which would mean his last name would go in as "de" only. Suboptimal, but might work fine for the OP.

  9. #24
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    Quote Originally Posted by anduril462 View Post
    and you're assuming the user is going to behave.
    This is something my C programming lecturer always drilled into our heads lol..
    "as a programmer, assume the user is dumb."
    Users never follow rules..and you assuming that they'll is bad, very bad.
    You ended that sentence with a preposition...Bastard!

  10. #25
    Registered User
    Join Date
    Jan 2011
    Posts
    3

    I stand corrected

    Use fgets

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  2. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  3. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM
  4. binary tree problem - help needed
    By sanju in forum C Programming
    Replies: 4
    Last Post: 10-16-2002, 05:18 AM
  5. Having problem deleting node in middle of stack
    By sballew in forum C Programming
    Replies: 3
    Last Post: 10-29-2001, 11:00 AM