Thread: Having trouble with scanf function

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    4

    Having trouble with scanf function

    Here are two functions for a simple program. I am trying to get information from the keyboard but for some reason the program does not take the info. In the Get_ballance function no matter what number is entered inD, and ballance ==0. For the Get_Trans function the program does not even pause for input and returns back to main.

    Thanks in advance,
    O'D

    The rest of the file is attached in case you need to look at it.




    /*------------------------------------------------------------------------------------------*/

    void Get_Ballance (double *Ballance)
    {
    double inD=0; /*temp variable*/
    printf("Enter the begining ballance: ");
    scanf ("%f",&inD);
    printf("%4.2f",inD);

    *Ballance = inD;

    /*test*/printf("\n Ballance = ");
    printf("%4.2f",*Ballance);
    printf("\n InD= ");
    printf("%4.2f",inD);

    }

    /*-------------------------------------------------------------------------------------------*/
    void Get_Trans (char *code,double *amount)
    {
    char inC;
    double inD;

    printf("\nChoose from the following options: ");
    printf("\nEnter 'C' for a check. ");
    printf("\nEnter 'D' for a deposit. ");
    printf("\nEnter 'E' to end.");
    scanf ("%c",&inC);
    printf("\n code = ");
    printf("%c",inC);
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > scanf ("%f",&inD);
    This should be
    scanf ("%lf",&inD);

    You're reading into a double.

    > For the Get_Trans function
    Before you do this...
    scanf ("%c",&inC);

    Do this
    while ( getchar() != '\n' ) continue;

    So your code looks like this
    while ( getchar() != '\n' ) continue;
    scanf ("%c",&inC);

    Some may tell you to use fflush(stdin), but they are wrong.

  3. #3
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    522
    aaarrrrggghhh scanf
    the way to use scanf is to not use it. When you use scanf with numbers the newline character is left in the input buffer, so the next time you call scanf it dosent read anything!!!!!
    an alternative is to use fgets followed by sscanf.
    Code:
    char temp[5];
    double InD;
    
    printf("\nEnter a number ");
    fgets(temp, sizeof(temp)-1, stdin);
    sscanf(temp, "%lf", &InD);
    This reads all input out of the input buffer.
    All spelling mistakes, syntatical errors and stupid comments are intentional.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 10:00 AM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM