Thread: scanf to read in whitespace characters

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    99

    scanf to read in whitespace characters

    hi, quick question. i want to read in a string using scanf and the printf before it says "enter the name of the team". but if enter manchester united it accepts it but the next question that comes up is "enter the name of the team""enter the name of the team". so it asks twice and accepts manchester united as two teams instead of one. how do i change this? thanks

  2. #2
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Can you post your code?

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    If your string has spaces you should be using fgets() to retrieve the string instead of scanf(). The scanf() function stops processing a string when it encounters a white space character.

    Jim

  4. #4
    Registered User
    Join Date
    Feb 2012
    Posts
    99
    Quote Originally Posted by jimblumberg View Post
    If your string has spaces you should be using fgets() to retrieve the string instead of scanf(). The scanf() function stops processing a string when it encounters a white space character.

    Jim
    ah ok thanks jim i didnt know that was the difference between them. i appreciate the help.

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    fgets will store the newline character which is something you'll likely have to find and handle accordingly. You can still use scanf if you know some of its tricks:

    Code:
    char str[40];
    
    printf("Enter some text: ");
    scanf("%39[^\n]",str);
    
    printf("You entered: \"%s\"\n",str);
    The scanf here will read and store everything up to 39 characters (save room for the terminating null) or until it comes across a newline character. This means it will accept multiple white space as long as it's not a newline.

    scanf to read in whitespace characters-untitled-jpg
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. scanf(), whitespace, and EOF
    By jason_m in forum C Programming
    Replies: 9
    Last Post: 09-13-2011, 12:51 AM
  2. Question about scanf and spaces or whitespace
    By jensbodal in forum C Programming
    Replies: 4
    Last Post: 11-26-2009, 04:12 PM
  3. stop scanf consming whitespace
    By mitchb in forum C Programming
    Replies: 2
    Last Post: 03-22-2009, 05:17 AM
  4. counting whitespace characters
    By artec_19 in forum C Programming
    Replies: 1
    Last Post: 10-30-2003, 02:50 PM
  5. Whitespace and scanf()
    By Procyon in forum C Programming
    Replies: 1
    Last Post: 01-05-2002, 01:55 AM