Using scanf function with strings

This is a discussion on Using scanf function with strings within the C Programming forums, part of the General Programming Boards category; Im just starting to learn about strings. Right now I'm just trying to prompt the user to input multiple words ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    15

    Using scanf function with strings

    Im just starting to learn about strings. Right now I'm just trying to prompt the user to input multiple words and then use the scanf function. Since I am just learning strings there may be other errors. Feel free to address those as well. Thanks.

    I can not figure out what to put after the %s in this situation. Or is a for loop not the right way to go about this problem?

    Code:
    int
    main()
    {
    	char *string1;
    	int i;
    	
    	for (i=0; i<9; i=i+1) 
    	{
    		printf("Please enter a word:\n");
    		scanf(%s,     );
    	}
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You can't scan for multiple words with %s in a single pass; you need a different format specifier for that. I suggest you read your C book and/or the man page for scanf.

    Your string isn't a string, it's just a pointer that points some random place. I suggest reading the FAQ on dynamic memory. Even if it was valid, you don't have an array of those pointers, so you couldn't do what your loop is trying.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,159
    fgets() is pretty much the de-facto standard for reading in a line of text. Whatever you do, don't ever use gets() as it's unsafe.
    Code:
    int main(void)
    {
      char strings[9][50];  // Allocate space for 9 words, up to 49 characters each (plus a '\0' terminator)
      int i;
    
      for(i=0;i < 9;++i)
      {
        puts("Please enter a word:");
        fgets(strings[i], 50, stdin);
      }
      return 0;
    }
    One thing to remember about fgets() is that it will leave the '\n' character at the end of the string from when the user pressed ENTER (assuming the buffer size was big enough to capture the entire input).
    If you understand what you're doing, you're not learning anything.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by laurenlb View Post
    Im just starting to learn about strings. Right now I'm just trying to prompt the user to input multiple words and then use the scanf function. Since I am just learning strings there may be other errors. Feel free to address those as well. Thanks.

    I can not figure out what to put after the %s in this situation. Or is a for loop not the right way to go about this problem?

    Code:
    int
    main()
    {
    	char *string1;
    	int i;
    	
    	for (i=0; i<9; i=i+1) 
    	{
    		printf("Please enter a word:\n");
    		scanf(%s,     );
    	}
    }
    Well, amongst the many possibilities you could do this...

    Code:
    int main (void) 
      { int i;
        char words[10][25] = {0};
    
       // gather input
        for (i = 0; I < 20; i++)
          { printf("Enter word #%d  :  ", i);
             scanf(" %24s", words[i]); }
    
       // display the results
       printf("\n\n");
       for (i = 0; i < 10; i++)
         printf("Word #%d is : %s\n",i, words[i]);
     
       return 0; }
    Go ahead try it out... it should work... but then go look up each function and study them until you understand what they do and how they contribute to the overall solution.

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    15
    Thanks. I understand now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  3. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 07:07 PM
  4. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 06:18 AM

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