Thread: Seems like correct code, but results are not right...

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    12

    Seems like correct code, but results are not right...

    I have some code, and while most of it works, the final part of it does not. I have looked for the mistake in the code for quite a while, but I have been unable to. (It's probably something obvious too...)

    Anyhow, here are the results of my last compile.

    #include <stdio.h>
    main()
    { int s_code, c_code;
    double sale_amount, sale_comm;
    printf("Salesperson code: ");
    scanf("%d", &s_code);
    printf("Customer code: ");
    scanf("%d", &c_code);
    printf("Sale amount in dollars and cents: ");
    scanf("%5.2lf", &sale_amount);
    printf("\n\n SALES REPORT");
    printf("\nSalesperson Code: %7d", s_code);
    printf("\n Customer Code: %7d", c_code);
    printf("\n\n Sale Amount: $%7.2lf", sale_amount);
    sale_comm = sale_amount * 0.144;
    printf("\n Commission: $%7.2lf", sale_comm);

    }

    Salesperson code: 14
    Customer code: 134
    Sale amount in dollars and cents: 24


    SALES REPORT
    Salesperson Code: 14
    Customer Code: 134

    Sale Amount: $ 0.00
    Commission: $ 0.00

    Obviously the sale amount and commission aren't supposed to be zero, but I have racked my brains for a long time on this problem. Thanks in advance for your help.

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Just use this:
    Code:
    scanf("%lf", &sale_amount);
    instead of %5.2lf
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >>scanf("%5.2lf", &sale_amount);
    Take out the 5.2 and it'll work fine :-)
    *Cela*

  4. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    12
    OMG, I feel sooooo stupid! >_<

    Thanks for your help ^_^.

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Please read this
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  2. Not returning correct results
    By drty2 in forum C Programming
    Replies: 4
    Last Post: 01-19-2009, 12:39 PM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 4
    Last Post: 01-16-2002, 12:04 AM