scanf question

This is a discussion on scanf question within the C Programming forums, part of the General Programming Boards category; K, I'm a TOTAL n00b(just started yesterday) to C, so excuse my ignorance! When trying to use this code: Code: ...

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    6

    scanf question

    K, I'm a TOTAL n00b(just started yesterday) to C, so excuse my ignorance!

    When trying to use this code:
    Code:
    scanf("%s", stuff);
    to get user input, it only grabs the first word! How would I grab all the words inputed? Thanks

  2. #2
    Registered User
    Join Date
    Oct 2002
    Posts
    98
    The best way would be to avoid scanf entirely. I would use fgets to capture everything that the user typed, then use strtok to split the string up into individual words, based on the assumption that the user will put spaces between the words!

    You're best off looking up how to use these functions yourself (they are in the stdio.h and string.h libraries) in order to learn how they work, but if you get stuck just show us what you tried!
    There is no such thing as a humble opinion

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    6
    Ok, thanks for the help!

  4. #4
    Registered User
    Join Date
    Oct 2003
    Posts
    6
    Originally posted by Morgan
    You're best off looking up how to use these functions yourself (they are in the stdio.h and string.h libraries) in order to learn how they work, but if you get stuck just show us what you tried!
    So were I am suppose to look up how to use them? In the files stdio.h and string.h? I looked at them and it all went in one side and straight out the other LOL

  5. #5
    Registered User
    Join Date
    Oct 2002
    Posts
    98
    Ok, look at these examples...

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
      char answer[100], *p;
    
      printf( "\nType something:\n" );
    
      fgets( answer, sizeof( answer ), stdin );
    
      if( ( p = strchr( answer, '\n' )) != NULL )
        *p = '\0';
    
      printf( "\nYou typed \"%s\"\n\n", answer );
    
      return 0;
    
    /* NOTE: strchr() returns a pointer to the first occurrence of the
    ** specified character in the specified string. strrchr() returns a pointer
    ** to the last specified character.
    ** In this case, we point p at the newline which fgets() automatically includes
    ** and then change it to be a proper string terminator i.e. '\0'.
    */
    }
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
      char sentance[100], *word;
    
      sprintf( sentance, "Just a few words" );
    
      word = strtok( sentance, " " );
    
      printf( "\n%s", word );
      
      while( ( word = strtok( NULL, " " )) != NULL )
        printf( "\n%s", word );
    
      printf( "\n\n" );
      return 0;
    }
    I didn't write the first one myself, but it's pretty standard code.

    Just mix and match!!
    There is no such thing as a humble opinion

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    >So were I am suppose to look up how to use them?

    The FAQ would be a good place to start.

    FAQ > How do I... (Level 1) > Get a line of text from the user/keyboard (C)
    FAQ > How do I... (Level 1) > How do I get a number from the user (C)
    FAQ > Other references > Misc reference sites > man pages
    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.*

  7. #7
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    Or you can just do this:
    Code:
    char string[100];
    
    scanf("%[^\n]",  string);  //continue to get input, until an ENTER is pressed
    With this you can also limit the number of chars:
    Code:
    char string[100];
    
    scanf("%99[^\n]", string);   // allows you to type in up to 99 chars
    Last edited by Devil Panther; 10-21-2003 at 01:52 PM.
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  8. #8
    Registered User
    Join Date
    Sep 2003
    Posts
    22
    Hello,I'm not sure why you guys have your scanf fucntion without the address operator
    scanf("%s", &var);
    I was referring to some parts of the code you have in the question, the scaf doesn't have the & operator, is that ok or what?

    Just a thought...just my thought...

  9. #9
    Registered User
    Join Date
    Sep 2003
    Posts
    22
    Hello,I'm not sure why you guys have your scanf fucntion without the address operator
    scanf("%s", &var);
    I was referring to some parts of the code you have in the question, the scaf doesn't have the & operator, is that ok or what?

    Just a thought...just my thought...

  10. #10
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Originally posted by dalguy2004
    Hello,I'm not sure why you guys have your scanf fucntion without the address operator
    scanf("%s", &var);
    I was referring to some parts of the code you have in the question, the scaf doesn't have the & operator, is that ok or what?
    Yes, it is correct without the &, because var is an array.
    Code:
    char buffer[20];
    scanf("%s", buffer);
    In the call to scanf, buffer "decays" into a pointer to the first element (it is the same as &buffer[0]). You want to pass to scanf a pointer to char. With the "decay", buffer does just that.

    It is not correct to use &buffer here because its type is pointer to array-of-char. It may be the same place in memory, but the expressions buffer (&buffer[0]) and &buffer do not have the same type.
    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.*

  11. #11
    Registered User
    Join Date
    Sep 2003
    Posts
    22

    scanf

    Hello Dave,
    Thanks so much for pointing that out. My eyes are wide open now..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 05-25-2006, 12:51 PM
  2. Exam Question - Possible Mistake?
    By Richie T in forum C++ Programming
    Replies: 15
    Last Post: 05-08-2006, 03:44 PM
  3. Scanf Question
    By shiyu in forum C Programming
    Replies: 4
    Last Post: 01-31-2003, 07:48 AM
  4. Simple question with scanf()
    By MadStrum! in forum C Programming
    Replies: 3
    Last Post: 01-20-2003, 09:41 AM
  5. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 02:57 PM

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