Thread: using scanf

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    60

    using scanf

    Hey, i have a question.
    I am using scanf to read in a paragraph from the keyboard and break it up into words.
    I am close to doing this but I cant figure out how to make my loop stop.
    When there are no more words, I would like my loop to stop, but I dont know how to check for that.
    Here is what I ahve so far, any help would be appreciated.


    Code:
    #include <stdio.h>
    #include <string.h>
    
    #define MAX_SIZE 16
    
    int main(void)
    {
    
    /* char array to hold each word with a max word size of 15 */
     char words[MAX_SIZE];
    
    	printf("Enter a line of text: "); 
    
    /* this loop prints each word one after the other from left to right */
    	while(scanf("%s", words)!= 0) {
                printf("%s\n", words);
    		/*if there are no more words, 
    		   end loop*/
    	}
    }
    How do you check to see if there are any more words left?
    Also how can I equal the value of scanf("%s", words) to 1 so the loop stops.
    Thanks

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,334
    Use fgets to read in the entire line, then use sscanf to read from there.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >When there are no more words, I would like my loop to stop, but I dont know how to check for that.
    scanf returns the number of successful conversions. If you're reading one word at a time, the number of conversions should be 1, so this will do what you want:
    Code:
    while ( scanf ( "%s", words ) == 1 )
      printf ( "%s\n", words );
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Sep 2008
    Posts
    60
    scanf returns the number of successful conversions. If you're reading one word at a time, the number of conversions should be 1, so this will do what you want:
    Code:
    while ( scanf ( "%s", words ) == 1 )
      printf ( "%s\n", words );
    Nothing changed. The loop still doesnt end. It just waits for more input.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    On Windows, press Control-Z, on Linux, press Control-D.

  6. #6
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Quote Originally Posted by tabstop View Post
    Use fgets to read in the entire line, then use sscanf to read from there.
    That is a better way, that I also recommend. Note that with fgets you also read the newline ('\n') character.

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Note that with fgets you also read the newline ('\n') character.
    With fgets(), you can also end up with a newline at the end of your buffer. You may not, however, if, for example, the line is longer than your buffer, or if the file doesn't end with an empty line (*shudder*) and you read this last line.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Registered User
    Join Date
    Sep 2008
    Posts
    60
    Quote Originally Posted by robwhit View Post
    On Windows, press Control-Z, on Linux, press Control-D.
    I need the loop to do that.

  9. #9
    Registered User
    Join Date
    Sep 2008
    Posts
    60
    This is a very simple task, why cant anyone give me a straight answer?

    example input:

    hello how are you?

    example output: //correct ouput

    hello
    how
    are
    you?
    //END program

    Right now I have output which is incorrect:

    hello
    how
    are
    you?
    _ //waitting for more input

    how do I terminate the loop after Ive read the last word?

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Press CTRL-Z. (Assuming you're on windows.)

    That's hold down the CTRL key, press the Z key, release the Z key, and then release the CTRL key.

    In other words, the user has to do it. Unless you always want to quit the program when "you?" is entered or something.

    [edit] The user has to indicate when the last word is read, because the computer can't tell the difference. [/edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  11. #11
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Why not just use fgets() to read a line at a time, and scan the line using sscanf() (If someone already suggested that, my bad I only read the first and last post).

  12. #12
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by gp364481 View Post
    I need the loop to do that.
    You need a loop to press keys on a keyboard?

  13. #13
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I think what the OP is trying to say is that he wants to automate the process of breaking the loop. Now that I opted to read other posts, I am the third person to suggest using fgets(). Plus if you just do as Prelude so eligantly suggested and specifically check for a valid input, you won't loop once data is input by the user. Just redesign your loop, my friend.

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    There are of course options other than ending the loop with CTRL-Z/D - you could for example state that a "." on it's own line or "end", "quit", "!quit" or something else is marking the end. But the computer doesn't know when you are finished without giving some sort of indication of "I'm finished".

    Obviously, the end-marker should be something that is unlikely to occur in normal sane input.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  15. #15
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Example:
    Code:
    printf("Enter a line of text: (Or if you are done and a Windows user, press CTRL+Z, unless of course you are on *nix or Mac. If that is the case, press CTRL+D)");
    Pretty smooth programming eh?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. scanf() consideres useless
    By Snafuist in forum C Programming
    Replies: 15
    Last Post: 02-18-2009, 08:35 AM
  2. Help with a basic scanf procedure.
    By killpoppop in forum C Programming
    Replies: 9
    Last Post: 11-03-2008, 04:39 PM
  3. Replies: 2
    Last Post: 02-20-2005, 01:48 PM
  4. Scanf and integer...
    By penny in forum C Programming
    Replies: 3
    Last Post: 04-24-2003, 06:36 AM
  5. scanf - data is "put back" - screws up next scanf
    By voltson in forum C Programming
    Replies: 10
    Last Post: 10-14-2002, 04:34 AM