Results 1 to 9 of 9

Thread: Scanf() Problem

  1. #1
    Just burned his ISO
    Join Date
    Jun 2009
    Posts
    9

    Default Scanf() Problem

    Hey I'm learning C and trying to do different problem sets current one that im working on is to take in 3 inputs and then display them out. im not sure what exactly in happening but when i try to put in my 2nd input it skips past the 3rd input and jumps directly to displaying any help in understanding why its not working right is appreciated
    Thanks
    heres the code

    #include"stdio.h"

    main(void)
    {
    int size =0;
    char sc1,sc2 ;

    // to ensure proper input
    do
    {
    printf("Please enter in size of square no bigger then 24 or smaller then 3:");
    scanf("%d",&size);
    }while((size >=24) ||(size<=2));


    // getting chars for nested sqares
    printf("Please enter in one special character:\n");
    scanf ("%c" ,&sc1);

    printf("Please enter in one more special character\n");
    scanf ("%c",&sc2);

    //displaying varibles
    printf("size %d\n",size ) ; printf("sc1 %c sc2 %c\n",sc1, sc2);

    return 0;
    }

  2. #2
    Very good friend of the forum Gitsnik's Avatar
    Join Date
    Jan 2010
    Location
    The Crystal Wind
    Posts
    851

    Default

    Your problem is that scanf is a PoS when it comes to reading numbers. Your safest/sanest bet is to blow it out to two lines, do an fgets into char buffer and then use sscanf to strip your number into your size variable. If my recollection of the function is correct (probably not this early on a saturday), it will look something like:
    Code:
    fgets(buffer, 5, stdin);
    sscanf(buffer, "%d", &size);
    Note that you will get some strange looking output when you request your character (an additional line), but fixing that is up to you.
    Still not underestimating the power...

    There is no such thing as bad information - There is truth in the data, so you sift it all, even the crap stuff.

  3. #3
    Just burned his ISO
    Join Date
    Jun 2009
    Posts
    9

    Default

    It grabs the number just fine. its the other two chars that its not handling so well. it gets the number then it skips the first request for a special char and goes on to the second one. compile the code and give it a shot to see what happenes

  4. #4
    Good friend of the forums
    Join Date
    Jun 2008
    Posts
    425

    Default

    It grabs the number just fine. its the other two chars that its not handling so well. it gets the number then it skips the first request for a special char and goes on to the second one. compile the code and give it a shot to see what happenes
    scanf might store it as a string(needs a null at the end), try %s instead of %c
    and char sc2[3];

  5. #5
    Very good friend of the forum Gitsnik's Avatar
    Join Date
    Jan 2010
    Location
    The Crystal Wind
    Posts
    851

    Default

    I am aware that it will grab the number just fine, I am also well aware that scanf is a piece of crap when it comes to doing things like this (not to mention some possible insecurities in its implementation.

    Splitting it out how I suggested is saner, faster, neater and makes you write better code. The fact that you are now a couple of days into this should be enough to prove this.
    Still not underestimating the power...

    There is no such thing as bad information - There is truth in the data, so you sift it all, even the crap stuff.

  6. #6
    Just burned his ISO
    Join Date
    Jun 2009
    Posts
    9

    Default

    I see that you are correct and after playing with it i get warnings but it works would you be able to tell me if im right as to what I'm thinking the first line fgets(buffer, 5, stdin); gets standard input and places it in to a buffer and im guessing that 5 is the max size that it will accept ?
    the second line sscanf(buffer, "%d", &size); this is pretty clear cut but because sscanf grabs input from buffers it puts it in to variables at there memory address that is probably a horrible way to put it but am i correct in my understanding?

  7. #7
    Very good friend of the forum Virchanza's Avatar
    Join Date
    Jan 2010
    Posts
    863

    Default

    Here's an explanation of the problem you're having:

    Question 12.18a

    Here's a better explanation:

    http://c-faq.com/stdio/gets_flush2.html
    Ask questions on the open forums, that way everybody benefits from the solution, and everybody can be corrected when they make mistakes. Don't send me private messages asking questions that should be asked on the open forums, I won't respond. I decline all "Friend Requests".

  8. #8
    Just burned his ISO
    Join Date
    Jan 2010
    Posts
    4

    Default

    You need to use fflush(stdin); after your scanf() to empty the input buffer

  9. #9
    Very good friend of the forum Virchanza's Avatar
    Join Date
    Jan 2010
    Posts
    863

    Default

    Quote Originally Posted by BuRner View Post
    You need to use fflush(stdin); after your scanf() to empty the input buffer
    I'm not going to deny that your solution works on more than one type of computer, but you should just know that the C Standard says flushing input streams is a no-no (to be pedantic, the behaviour is "undefined", meaning your program can crash if it wants to).

    Here's a quick discussion of it:

    Things to Avoid in C/C++ -- fflush(stdin), Part 2 - GIDNetwork

    And here's an alternative way of flushing an input stream that will work on all kinds of computer:

    Code:
    int ch = 0;
    
    while((ch = getc(fp)) != EOF && ch != '\n')
    {
        /* Empty body */;
    }
    Of course to make it neater you can make a function out of it:

    Code:
    int FlushInputStream(FILE *const p)
    {
         int ch = 0;
    
         while( (ch = getc(p)) != EOF && ch != '\n')
         {
             /* Empty body */
         }
    
         return EOF == ch;
    }
    Then you could call this function after you call scanf.
    Ask questions on the open forums, that way everybody benefits from the solution, and everybody can be corrected when they make mistakes. Don't send me private messages asking questions that should be asked on the open forums, I won't respond. I decline all "Friend Requests".

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •