Thread: Scanf() skipped

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    17

    Scanf() skipped

    This is my program code for the main.c
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include "test.h"
    
    int main()
    {
        float a,b,c;
        int s,g=1;
        char ch;
    while(g)
    {
        printf("\nArea of:\n1.Triangle\n2.Square\n3.Circle\n4.Convert letter\nplease choose:");
        scanf("%d",&s);
    
        switch(s)
        {
            case 1:
            printf("\nlength:");
            scanf("%f",&a);
            printf("width:");
            scanf("%f",&b);
            c=TRI(a,b);
            printf("The area is %f",c);
            break;
            case 2:
            printf("\nlength:");
            scanf("%f",&a);
            printf("width:");
            scanf("%f",&b);
            c=SQU(a,b);
            printf("The area is %f",c);
            break;
            case 3:
            printf("\ndiameter:");
            scanf("%f",&a);
            c=CIR(a);
            printf("The area is %f",c);
            break;
            case 4:
            printf("Upper Case Letter");
            scanf("%c",&ch);
            ch=CONV(ch);
            printf("\nLower Case Version: %c",ch);
            break;
            default:
            printf("YOU PICKED WRONG NUMBER\n");
            continue;
        }
        printf("\ndo you want to use the program again(1=yes/0=no)?");
        scanf("%d",&g);
    }
    return 0;
    
    }
    and this is for "test.h"
    Code:
    #ifndef test_h
    #define test_h
    
    #define TRI(a,t) (0.5*a*t)
    #define SQU(p,l) (p*l)
    #define CIR(r) (0.25*3.14*r*r)
    #define CONV(c) (c+32)
    
    #endif
    when i compile it, there isn't any warning or error.
    but when i run the program, i choose menu 4(Convert Letter),the program skipped the scanf(), so i didn't have the chance to input the letter.
    can someone help me please?i don't know what's wrong with my code

    *note:don't mind with the Macro abuse. it's just an exercise.

  2. #2
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    Well, I haven't gone through your code, but most scanf skipping problems are related to the input buffer not being flushed. Heres what I mean -

    Cprogramming.com FAQ > Flush the input buffer

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    scanf() doesn't include the newline with certain kinds of data, so when you type

    4\n

    which that '\n' is the ENTER key, it may get kept in the stdin buffer until the next scanf() call, which is triggered with a single newline.

    The problem is probably a "%c" and not "%i", but if you think you know the line try a dummy getchar() call after it:
    Code:
    getchar();
    If that solves the problem, include \n there
    Code:
    scanf("%c\n", &ch);
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    17
    @spidey: i still don't understand about code and the explanation in the FAQ that you showed to me. sorry, i'm totally a newbie. is there any better way to make me understand it ? but, thnks for your help before.

    ---------------------------------------------------------------------------------------------------------------

    @MK27: getchar doesn't solve the problem.
    but, i use your method to input "\n", then the program become messy.

    the program run like this

    Code:
    Area of:
    1.Triangle
    2.Square
    3.Circle
    4.Convert Letter
    please choose:4
    Upper Case Letter:A
    
    Lower Case version: *
    do you want to use the program again(1=yes/0=no)?
    Area of:
    1.Triangle
    2.Square
    3.Circle
    4.Convert Letter
    please choose:Upper Case Letter:
    the program doesn't wait me to confirm whether i use the program again or not. it directly shows the menu again. and the program also doesn't wait the input for menu selection
    and the conversion to low case letter doesn't works.
    but,when i input any character after "please choose:Upper Case Letter:". it happened like this:
    Code:
    Area of:
    1.Triangle
    2.Square
    3.Circle
    4.Convert Letter
    please choose:4
    Upper Case Letter:A
    
    Lower Case version: *
    do you want to use the program again(1=yes/0=no)?
    Area of:
    1.Triangle
    2.Square
    3.Circle
    4.Convert Letter
    please choose:Upper Case Letter:B
    
    Lower Case version: a
    do you want to use the program again(1=yes/0=no)?
    Area of:
    1.Triangle
    2.Square
    3.Circle
    4.Convert Letter
    please choose:Upper Case Letter:
    the conversion works,but it messy.
    it makes me feel more confused.
    what's going on with the program?

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    MK got his left and right mixed up -- "%c\n" will read a character (in this case, still the carriage return and then requires another carriage return afterwards (hence the printing of a blank line -- because that's the input) and the weirdness after).

    What you want instead is to use " %c" (notice the space between the quote and the % sign) to get rid of carriage-returns before the input.

  6. #6
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by new-b View Post
    This is my program code for the main.c

    when i compile it, there isn't any warning or error.
    but when i run the program, i choose menu 4(Convert Letter),the program skipped the scanf(), so i didn't have the chance to input the letter.
    can someone help me please?i don't know what's wrong with my code

    *note:don't mind with the Macro abuse. it's just an exercise.
    When you give your choice as 4 as here
    Code:
    Area of:
    1.Triangle
    2.Square
    3.Circle
    4.Convert Letter
    please choose:4
    After entering 4, you have pressed the ENTER key,which is nothing but the '\n' in C. This '\n' gets stored in the buffer and the next scanf reads it, that's why it doesn't ask for the character you want from the input coz it has already taken it from the buffer(which is '\n'). To avoid this just do what tabstop has suggested i.e including a space between " and % like this.
    Code:
    scanf(" %c",&ch);
    This space eats up the '\n' stored in the buffer and results in asking the character from the keyboard.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  7. #7
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    What Ben10 says, but I would add that you indent a bit within your switch case statement. Your code is all bunched together.

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    17
    oh,i understand it and the "buffer" things now.
    thanks for your (all of you who reply my thread) help.

    but, i still don't understand clearly why the "space" in scanf() can eat up the "/n".
    i tried to use getchar() (like MK27 said),but i put it before scanf() to fix the ENTER problem, in this case i still understand about what the getchar( ) did.
    but nit with the "blank space" in scanf().
    how does it work?

  9. #9
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    but nit with the "blank space" in scanf().
    how does it work?
    Its basically the same concept as getchar(). But adding a space before the % causes it to skip the space.
    Just think of it as if the newline and the space cancel each other out, and then the input is read by scanf.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I just hope that you know that '\n' and '/n' aren't the same thing.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. scanf is being skipped
    By yougene in forum C Programming
    Replies: 6
    Last Post: 12-24-2008, 06:05 AM
  2. Second scanf keeps getting skipped.
    By yougene in forum C Programming
    Replies: 9
    Last Post: 12-23-2008, 02:39 AM
  3. Help with a basic scanf procedure.
    By killpoppop in forum C Programming
    Replies: 9
    Last Post: 11-03-2008, 04:39 PM
  4. scanf issue
    By fkheng in forum C Programming
    Replies: 6
    Last Post: 06-20-2003, 07:28 AM
  5. scanf - data is "put back" - screws up next scanf
    By voltson in forum C Programming
    Replies: 10
    Last Post: 10-14-2002, 04:34 AM