Thread: Read two words separated by a space with scanf at once

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    204

    Read two words separated by a space with scanf at once

    Is it possible to read a name like Max Payne with scanf using a single array of characters? Thanks.

  2. #2
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    scanf("%s%s", maxVar, payneVar);

    Free code: http://sol-biotech.com/code/.

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    204
    I knew that. I said I wanted to do it using a single array of characters. I know I can use fgets and the like but I'm just curious

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    char name[80], temp[80];
    scanf("%s%s", name, temp);
    strcat(name, " ");
    strcat(name, temp);
    That's how I would do it.

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    scanf("%s%s"

    Never user %s (or %[) without specifying a width.
    http://www.eskimo.com/~scs/C-faq/q12.20.html

    [edit]
    Is it possible to read a name like Max Payne with scanf using a single array of characters?
    Do you mean to keep the space, like this?
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
       char name[80];
       if ( scanf("%79[^\n]", name) == 1 )
       {
          printf("name = \"%s\"\n", name);
       }
       return 0;
    }
    
    /* my output
    Max Payne
    name = "Max Payne"
    */
    Last edited by Dave_Sinkula; 06-23-2005 at 04:26 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can scanf read from a stored variable?
    By azrael in forum C Programming
    Replies: 7
    Last Post: 03-20-2009, 03:54 AM
  2. Need help in debugging this code
    By jaggesh in forum C Programming
    Replies: 4
    Last Post: 02-09-2008, 08:47 AM
  3. how to obtain first character of every other word
    By archie in forum C++ Programming
    Replies: 8
    Last Post: 02-18-2002, 01:58 PM
  4. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 10:52 AM
  5. Galaxian or Space invaders for my next game (read this)
    By Leeman_s in forum C++ Programming
    Replies: 7
    Last Post: 11-07-2001, 09:28 PM