Thread: my program skips the second scanf. pls help!

  1. #1
    Registered User
    Join Date
    Jan 2016
    Posts
    1

    Question my program skips the second scanf. pls help!

    So i litterally started the CS50 courses yesterday, and ive got problems with one of the programs i need to make. Its basically just a basic code, that calculates how much water you use per minute in the shower. That part works but apparently it also need to work if someone types some random numbers and letter into it by printing "Retry:". heres the coding ive done so far. Also this is in C.
    Code:
    #include <stdio.h>
    #include <math.h>
    
    
    int main(void)
    {
        int a, b, c, d;
        printf("minutes: ");
        scanf("%d", &a);
        getchar();
        b=a * 12;
        if (b<=0)
        {
            printf("Retry: ");
            scanf("%d", &c); <- it skips here
            d=c * 12;
            printf("\nbottles: %d\n", d);
        }
        else
        {
            printf("bottles: %d\n", b);
        }
    }

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,731
    Yeah, see, scanf leaves the input buffer as-is if it doesn't manage to read the input you asked for. So, you'll have to clear it yourself. A simple while loop checking getchar() should be enough. Remember that getchar() can return EOF as well as characters.
    Devoted my life to programming...

  3. #3
    Ticked and off
    Join Date
    Oct 2011
    Location
    La-la land
    Posts
    1,728
    I so wish learners were taught to check the return value of scanf() family of functions. It's the number of successful conversions (could be zero), or EOF (end of input, matching failure if pattern contains characters to match, or a read error occurs).

    So, for example,
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    int main(void)
    {
        int minutes;
    
        while (sscanf("%d", &minutes) == 1 && minutes > 0)
            printf("%d minutes yields %d bottles\n", minutes, 12 * minutes);
    
        return EXIT_SUCCESS;
    }
    will read integers from standard input, separated by whitespace (spaces, tabs, or newlines) until non-integer input is read, or the number is less than one (not greater than zero).

  4. #4
    Registered User
    Join Date
    Sep 2015
    Posts
    14
    it works for me. it doesn't skip anything.

  5. #5
    Ticked and off
    Join Date
    Oct 2011
    Location
    La-la land
    Posts
    1,728
    Quote Originally Posted by endrick View Post
    it works for me. it doesn't skip anything.
    It will, if you input something other than a number, say the letter x. You should actually read the original question:
    it also need to work if someone types some random numbers and letter into it by printing "Retry:"
    The "skips here" comment is related to invalid, non-numeric input.

  6. #6
    Registered User
    Join Date
    Jan 2016
    Posts
    12
    So i litterally started the CS50 courses yesterday

    I started the CS50 courses to

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program skips scanf
    By nic050 in forum C Programming
    Replies: 2
    Last Post: 06-05-2015, 10:54 PM
  2. scanf skips lines of code in DOS using Dev-C++ and Windows XP
    By jenovanomusuko in forum C Programming
    Replies: 9
    Last Post: 12-21-2008, 03:10 AM
  3. program skips over code
    By willc0de4food in forum C Programming
    Replies: 9
    Last Post: 11-16-2006, 06:38 PM
  4. First scanf() skips next scanf() !
    By grahampatten in forum C Programming
    Replies: 5
    Last Post: 08-17-2004, 02:47 AM
  5. my program skips scanf and getchar()
    By jk81 in forum C Programming
    Replies: 15
    Last Post: 11-29-2002, 05:54 PM