Thread: sscanf

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    8

    sscanf

    I am trying to read in a user defined string. The user will enter two floats, and a name. The three items are separated by a single space. I'm trying to break up the three by sscanf and save the two numbers as separate floats and the name as a string. I know this doesn't work, Is the only way to use the single spaces to my advantage and break (the user defined entered things) the string up and read the parts I want and assign them separately!

    thanks for looking......cheers


    Code:
     printf("\nPlease type in two float values and one of the first names,\n");
    
    printf("followed by  all of which are separated by a single space:\n");
    
    sscanf("%f","%f","%s",float &num1, float &num2, char *p_user);

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Code:
    #include <stdio.h>
    
    int main ( void )
    {
      float a, b;
      char s[BUFSIZ];
    
      fgets ( s, sizeof s, stdin );
      sscanf ( s, "%f%f%s\n", &a, &b, s );
    
      printf ( "%f -- %f -- %s", a, b, s );
    
      return 0;
    }
    -Prelude
    My best code is written with the delete key.

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>sscanf ( s, "%f%f%s\n", &a, &b, s );
    What, no return code checking... shame on you
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >What, no return code checking... shame on you
    Lazy, no other excuse.

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sscanf and string handling question
    By ursula in forum C Programming
    Replies: 14
    Last Post: 05-30-2009, 02:21 AM
  2. Problem using sscanf fgets and overflow checking
    By jou00jou in forum C Programming
    Replies: 5
    Last Post: 02-18-2008, 06:42 AM
  3. Problems reading formatted input with sscanf
    By Nazgulled in forum C Programming
    Replies: 17
    Last Post: 05-10-2006, 12:46 AM
  4. sscanf question
    By Zarkhalar in forum C++ Programming
    Replies: 6
    Last Post: 08-03-2004, 07:52 PM
  5. sscanf (I think)
    By RyeDunn in forum C Programming
    Replies: 7
    Last Post: 07-31-2002, 08:46 AM