scanf to read in whitespace characters

This is a discussion on scanf to read in whitespace characters within the C Programming forums, part of the General Programming Boards category; hi, quick question. i want to read in a string using scanf and the printf before it says "enter the ...

  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,323
    Can you post your code?

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    2,698
    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,794
    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.

    Name:  untitled.jpg
Views: 253
Size:  21.9 KB
    "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, 03: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, 01:50 PM
  5. Whitespace and scanf()
    By Procyon in forum C Programming
    Replies: 1
    Last Post: 01-05-2002, 12:55 AM

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