Thread: Need help with scanf and white spaces

  1. #1
    Registered User
    Join Date
    Sep 2017
    Posts
    2

    Need help with scanf and white spaces

    Hi, I'm trying to write a program that has several user inputs and will then print out the results. The problem I'm having is when someone enters a first and last name, the last name won't print. I know that scanf stops when it comes across a white space. I've tried changing it to fgets() but that hasn't worked. I've also tried some of the online suggestions for scanf like scanf("%[^\n]s", name) but it fails as well. Any help would be appreciated.
    Here's the code:

    Code:
    #include <stdio.h>
    #include <string.h>
    int main()
    {
        float number, number1;
        number;
        printf("Number of classes this semester: ");
        scanf("%f", &number);
     
        number1;
        printf("Credits this semester: ");
        scanf("%f", &number1);
    
    
        char name[30];
        printf("Enter your full name, first and last: ");
        scanf("%s", &name);
    
    
        printf("Classes: %f\n", number);
        printf("Credits: %f\n", number1);
        printf("Name: %s\n", name);
    
    
        return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    The problems you're having with fgets() is being caused because you used scanf() prior to the fgets(). The scanf() function leaves the delimiting character in the end of line character in the input buffer which fgets() takes as a complete entry. To solve the problem you need to remove the end of line character before you call fgets().


    Jim

  3. #3
    Registered User
    Join Date
    Sep 2017
    Posts
    2
    Quote Originally Posted by jimblumberg View Post
    The problems you're having with fgets() is being caused because you used scanf() prior to the fgets(). The scanf() function leaves the delimiting character in the end of line character in the input buffer which fgets() takes as a complete entry. To solve the problem you need to remove the end of line character before you call fgets().


    Jim
    Thank you Jim. I placed fgets() ahead of scanf() and it works. My other question then would be how would I remove the end of line character before calling fgets, if I want to keep the same order of inputs?

    Code:
    #include <stdio.h>
    #include <string.h>
    int main()
    {
        float number, number1;
    
    
        char name[30];
        printf("Enter your full name, first and last: ");
        fgets(name, 30, stdin);
        
        number;
        printf("Number of classes this semester: ");
        scanf("%f", &number);
     
        number1;
        printf("Credits this semester: ");
        scanf("%f", &number1);
    
    
        printf("Name: %s\n", name);
        printf("Classes: %f\n", number);
        printf("Credits: %f\n", number1);
    
    
        return 0;
    }

  4. #4
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char **argv)
    {
     char Fname [10];
     char Lname[10];
    
        printf("Enter your full name, first and last: ");
    
            scanf("%s ", Fname);
            scanf("%s", Lname);
    
            printf("%s %s \n", Fname, Lname);
    
    return 0;
    }
    out put
    Code:
    userx@~/bin <> ./a.out
    Enter your full name, first and last: bob yoda
    bob yoda 
    userx@~/bin <>
    yours
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char **argv)
    {
     char Fname [10];
     char Lname[10];
     float number, number1;
     
        printf("Enter your full name, first and last: ");
     
            scanf("%s ", Fname);
            scanf("%s", Lname);
    
        printf("Number of classes this semester: ");
        scanf("%f", &number);
    
     
        printf("Credits this semester: ");
        scanf("%f", &number1);
    
       printf("%s %s \n", Fname, Lname);
        printf("Classes: %f\n", number);
        printf("Credits: %f\n", number1);
     
    return 0;
    }

    out put
    Code:
    userx@~/bin <> ./a.out
    Enter your full name, first and last: bob yoda
    Number of classes this semester: 3000.2
    Credits this semester: 9192923.444
    bob yoda 
    Classes: 3000.199951
    Credits: 9192923.000000
    Last edited by userxbw; 09-22-2017 at 12:46 PM.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    And another buffer overrun in waiting. Never NEVER use a function to retrieve a string that doesn't limit the number of characters it will try to retrieve! For scanf() you need to use the proper width specifier to limit the number of characters it will try to retrieve, and note that unlike fgets() the scanf() function will stop processing a string when it encounters a whitespace character.


    Code:
    #include <stdio.h>
     
    // int main(int argc, char **argv) // Not using any command line arguments so don't use the parameters.
    
    int main(void)
    {
        char Fname [10];
        char Lname[10];
     
        printf("Enter your full name, first and last: ");
     
        scanf("%9s", Fname);
        scanf("%9s", Lname);
        
        printf("%s %s \n", Fname, Lname);
     
        return 0;
    }
    @userxbw you really should stop foisting such poor code on unsuspecting newbies.

    Jim
    Last edited by jimblumberg; 09-22-2017 at 12:42 PM.

  6. #6
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by jimblumberg View Post
    And another buffer overrun in waiting. Never NEVER use a function to retrieve a string that doesn't limit the number of characters it will try to retrieve! For scanf() you need to use the proper width specifier to limit the number of characters it will try to retrieve, and note that unlike fgets() the scanf() function will stop processing a string when it encounters a whitespace character.


    Code:
    #include <stdio.h>
     
    // int main(int argc, char **argv) // Not using any command line arguments so don't use the parameters.
    
    int main(void)
    {
        char Fname [10];
        char Lname[10];
     
        printf("Enter your full name, first and last: ");
     
        scanf("%9s", Fname);
        scanf("%9s", Lname);
        
        printf("%s %s \n", Fname, Lname);
     
        return 0;
    }
    @userxbw you really should stop foisting such poor code on unsuspecting newbies.

    Jim
    c - using scanf function with pointers to character - Stack Overflow

    besides that lead to what? you actually showing him how to get out of mixing functions to get the same thing done. and I just learned how to put in size - one for end \0 thanks because I just figured out that call it twice thing off by reading a little bit up on it off that link then doing that instead after seeing what only using one did, so I used two and it worked then you came and corrected me and I Thank you, now i know more. again mixing get and scan or mixing functions that do like things to me is just making more work for oneself. after reading up on it a min ago seeing everything one got a do using fgets malloc and what nots to get that 'n' out of there to move on.

    argc and argv is a mute point.

    as far as the buffer over flow again thing did you go abck and look at it, he was using two loops inner loop over riding outer loop so it was you (?) letting him over run EOF instead.
    Last edited by userxbw; 09-22-2017 at 01:19 PM.

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    What is the point of that link?

    besides that lead to what?
    Overflowing a buffer leads to undefined behaviour, which means anything can happen from appearing to operate correctly to formatting your hard disk.

    you actually showing him how to get out of mixing functions to get the same thing done.
    This makes absolutely no sense. Perhaps you would care to clarify what you mean.

    There is nothing wrong with using both fgets() and scanf() together as long as you handle the problem of the new line character being left in the input buffer when switching from scanf() to fgets(). You are usually better off using fgets() when retrieving strings, since fgets() requires the limiter, unlike scanf() where the limiter is optional.

  8. #8
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by jimblumberg View Post
    What is the point of that link?


    Overflowing a buffer leads to undefined behaviour, which means anything can happen from appearing to operate correctly to formatting your hard disk.


    This makes absolutely no sense. Perhaps you would care to clarify what you mean.

    There is nothing wrong with using both fgets() and scanf() together as long as you handle the problem of the new line character being left in the input buffer when switching from scanf() to fgets(). You are usually better off using fgets() when retrieving strings, since fgets() requires the limiter, unlike scanf() where the limiter is optional.
    he is scaning the cli for input, using fgets and scanf . what problems was he running into by mixing the two functions that get information off the cli? doing it my way and adding your fix to it saved him from doing what?
    having to search the input of fgets for that \n and getting rid of it first before moving on. I discovered that using scanf twice just picks up what is needed without that

    you tell him to
    The problems you're having with fgets() is being caused because you used scanf() prior to the fgets(). The scanf() function leaves the delimiting character in the end of line character in the input buffer which fgets() takes as a complete entry. To solve the problem you need to remove the end of line character before you call fgets().
    when from you correcting me you obviously know how to use scanf with char[size] properly, instead of leading him down that path you created more work for him by just telling him to remove that char before calling fgets, leaving him with a big question mark in his head,
    My other question then would be how would I remove the end of line character before calling fgets, if I want to keep the same order of inputs?
    and not just showing him what I did, while adding what i didn't.

    now he has a working program without all of that extra code needed to remove what you told him he had to do.

    that link is where i got the idea of what I posted.
    But for scanf and printf to work you must have this at the top of your program:
    #include <stdio.h>
    #include <stdlib.h>
    after trying that
    Code:
    char arrays[12];
    char *pointers;
    pointers = arrays;
    scanf("%s",pointers);
    then just removing that and doing what I did instead.to get both the first and last name. because I seen what scanf was doing, just going to the space, so I put it again and it got both, problem solved. minus one mishaps I didn't know about. adding the number assigned minus one for end \0

    now he doesn't have to try and figure out how to get fgets and scaf to work happily together when they do not even have to work together.

    I believe one should use one or the other that way you're not stuck with trying to make them compatible with each other. as reading what you stated shows one gives the other one problems.
    Last edited by userxbw; 09-22-2017 at 01:45 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Emit white spaces, when you hit tab. Why?
    By std10093 in forum Tech Board
    Replies: 4
    Last Post: 01-10-2014, 08:44 PM
  2. Reading white spaces? oh and scanf_s
    By RobotGymnast in forum C++ Programming
    Replies: 7
    Last Post: 11-01-2008, 05:32 AM
  3. white spaces ...
    By eagles in forum C++ Programming
    Replies: 6
    Last Post: 01-13-2006, 07:55 AM
  4. white spaces
    By Jules in forum C++ Programming
    Replies: 2
    Last Post: 04-24-2004, 02:55 PM
  5. White spaces
    By Garfield in forum A Brief History of Cprogramming.com
    Replies: 34
    Last Post: 11-18-2001, 08:24 PM

Tags for this Thread