Thread: problem with strings (explained)

  1. #1
    Registered User dune911's Avatar
    Join Date
    Sep 2001
    Posts
    140

    problem with strings (explained)

    hi - i have a problem using strings in text output.

    when i put the following code in my 'play'-function,

    [...]

    char name[20]; /* at top of the function */

    printf("\n Hello! What is your name? > ");
    scanf("%s",&name[0]);
    printf("\n Ok, %s, let's get it on...\n\n",name);

    [...]

    then the program is running until you enter
    a name like "Peter Pan" with a space inside.
    Now the program would run once displaying
    "Ok, Peter, ..." and after that (without
    entering the 'again'-question) it runs with
    "Ok, Pan, ...". and this way i don't want
    to include this code in my program.

    when i try this:

    [...]

    char name[20]; /* at top of the function */

    printf("\n Hello! What is your name? > ");
    gets(name);
    printf("\n Ok, %s, let's get it on...\n\n",name);

    [...]

    the program is not working. it runs, but you won't
    be able to enter your name. the program just displays
    the text out of 'printf' and just puts a space instead
    of the %s data.

    can someone explain why this happens?

    at last i tried this:

    [...]

    char name[20]; /* at top of the function */

    printf("\n Hello! What is your name? > ");
    gets(name);
    printf("\n Ok, ");
    puts(name);
    printf(", let's get it on...\n\n");

    [...]

    but puts can't display anything, when gets won't work.

    thanks for any help!

  2. #2
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,212
    Code:
    #include <stdio.h>
    
    int main(void)
    {
     char name[BUFSIZ];
     char surname[BUFSIZ];
    
     printf("Hello.  What is your name? \n> ");
     scanf("%s %s",&name,&surname);
     printf("Hello, %s %s, let's get it on...",name,surname);
     return 0;
    }
    Is this one of those virtual sluts?
    Last edited by Brian; 01-20-2002 at 11:51 AM.

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    Your buffer still contains a '\n' at that point in the program it seems. Flush your buffer and it should work....

    Code:
    [...]
    for (;getchar != '\n';);
    gets(name);
    [...]
    Callou collei we'll code the way
    Of prime numbers and pings!

  4. #4
    The Artful Lurker Deckard's Avatar
    Join Date
    Jan 2002
    Posts
    633
    FYI, the address-of operator (&) is not required when passing an array to scanf().
    Jason Deckard

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > for (;getchar != '\n';

    for ( ; getchar() != '\n'; ) ;
    Make it a call...

  6. #6
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,212
    Originally posted by Deckard
    FYI, the address-of operator (&) is not required when passing an array to scanf().
    sorry, habit.

  7. #7
    Registered User
    Join Date
    Feb 2002
    Posts
    11
    What if thier isn't 2 names? (In the surname exaple). Will it just ignore the second %s?

  8. #8
    Registered User samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Newport
    Posts
    382
    I had PRECISELY the same problem. Use gets () and all your problems will be solved!!! Also:

    &n[0] == n
    Current Setup: Win 10 with Code::Blocks 17.12 (GNU GCC)

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > Use gets () and all your problems will be solved
    Certainly solves all the problems for anyone wanting to smash your program with a buffer overflow

    Use fgets
    http://www.eskimo.com/~scs/C-faq/q12.23.html

  10. #10
    Registered User samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Newport
    Posts
    382
    Yes, but this is not operating system code we are writing, here - although I see your point. The gets bug was the cause of the Unix worm - it's being discontinued.

    Perhaps you'd better use fgets instead.
    Current Setup: Win 10 with Code::Blocks 17.12 (GNU GCC)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compare strings problem
    By chelito19 in forum C Programming
    Replies: 2
    Last Post: 04-16-2009, 08:01 PM
  2. Problem with comparing strings!
    By adrian2009 in forum C Programming
    Replies: 2
    Last Post: 02-28-2009, 10:44 PM
  3. Problem with Strings and Conversions
    By patso in forum C Programming
    Replies: 8
    Last Post: 04-09-2008, 12:01 PM
  4. problem with strings
    By agentsmith in forum C Programming
    Replies: 5
    Last Post: 04-08-2008, 12:07 PM
  5. copy strings problem
    By dgcampos in forum C++ Programming
    Replies: 4
    Last Post: 04-23-2004, 08:05 PM