Ultra noob programming help regarding printf and floats!

This is a discussion on Ultra noob programming help regarding printf and floats! within the C Programming forums, part of the General Programming Boards category; Hi all, I am having a hard time with some of my homework, specifically regarding how to printf floats. I ...

  1. #1
    Who
    Who is offline
    Registered User
    Join Date
    Jan 2013
    Posts
    21

    Cool Ultra noob programming help regarding printf and floats!

    Hi all, I am having a hard time with some of my homework, specifically regarding how to printf floats. I can't seem to print the number i want out using float, it just becomes a jumbled mess.
    Code:
    #include <stdio.h>
    #define TICKER "LRCX"
    #define PURCHASE_DATE "01/02/13"
    #define SELL_DATE "01/30/13"
    #define INVESTMENT_AMOUNT "10,000.00"
    
    
    int main ()
    {
        printf("HELLO!\n");
        printf("Assignment #3\n");
        printf("Code::Blocks\n");
    
    
    //Local Declarations
    float purchase_price;
    float sell_price;
    float percentage;
    float shares_purchased;
    float am_gained_loss;
    float shares_sold;
    //Statements
        printf("Enter share purchase price for LRCX=>");
        scanf("%f",&purchase_price);
    
    
        printf("Enter the selling price for LRCX=>");
        scanf("%f",&sell_price);
        printf("\n");
    
    
    //Calculations
    shares_purchased = 10000 / purchase_price;
    shares_sold = 10000 / sell_price;
    am_gained_loss = purchase_price - sell_price;
    percentage = purchase_price / sell_price;
    
    
    //Result
        printf("Stock: " TICKER"\n");
        printf("Buy Date: " PURCHASE_DATE"\n");
        printf("Sell Date: " SELL_DATE"\n");
        printf("Buy Share Price: %.2f\n", &purchase_price);
        printf("Sell Share Price: %.2f\n", &sell_price);
        printf("Shares Purchased: %.2f\n",&shares_purchased);
        printf("\n");
    
    
        printf("Amount of Investment: "INVESTMENT_AMOUNT);
        printf("\n");
    
    
        printf("Value of Shares Sold:%.2f",&shares_sold);
        printf("\n");
    
    
        printf("Amount of Gain/Loss:%.2f\n", &am_gained_loss);
        printf("Percent Gain/Loss:%.2f%%\n",&percentage);
        printf("\n");
    
    
    return 0;
    }
    Thats the code I currently have, I've probably tried everything to get the number to come out, but I just cant seem to figure it out. It should look like this, but with different numbers and stock:

    Stock: MCD
    Buy Date: 01/02/13
    Sell Date: 01/29/13
    Buy Share Price: $89.40
    Sell Share Price: $91.50
    Shares Purchased: 111.86

    Amount of Investment: $10,000.00
    Value of Shares Sold: $10,234.90
    Amount of Gain/Loss: $234.90
    Percent Gain/Loss: 2.35%

    However, this is how mine turns out:

    HELLO!
    Assignment #3
    Code::Blocks
    Enter share purchase price for LRCX=>23
    Enter the selling price for LRCX=>23


    Stock: LRCX
    Buy Date: 01/02/13
    Sell Date: 01/30/13
    Buy Share Price: -1.#R
    Sell Share Price: -1.#R
    Shares Purchased: -1.#R


    Amount of Investment: 10,000.00
    Value of Shares Sold:-1.#R
    Amount of Gain/Loss:-1.#R
    Percent Gain/Loss:-1.#R%




    Process returned 0 (0x0) execution time : 2.864 s
    Press any key to continue.


    I apologize if this post is long, but I really need help on what I'm doing wrong. I probably spent at least two hours trying to figure this out and trying different things besides float like, double, and int.

    Thanks guys!

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    2,380
    You should be getting compiler errors.

    Code:
    main.c||In function 'main':|
    main.c|43|warning: format '%.2f' expects type 'double', but argument 2 has type 'float *'|
    main.c|44|warning: format '%.2f' expects type 'double', but argument 2 has type 'float *'|
    main.c|45|warning: format '%.2f' expects type 'double', but argument 2 has type 'float *'|
    main.c|53|warning: format '%.2f' expects type 'double', but argument 2 has type 'float *'|
    main.c|57|warning: format '%.2f' expects type 'double', but argument 2 has type 'float *'|
    main.c|58|warning: format '%.2f' expects type 'double', but argument 2 has type 'float *'|
    ||=== Build finished: 0 errors, 6 warnings ===|
    These tell you exactly where to look.

    Remove the '&' from before the variable names in your "printf()" statement - it is incorrect to include them in this context.

    i.e.

    Code:
    printf("Buy Share Price: %.2f\n", &purchase_price);
    
    // should be
    
    printf("Buy Share Price: %.2f\n", purchase_price);

  3. #3
    Who
    Who is offline
    Registered User
    Join Date
    Jan 2013
    Posts
    21
    YAY! I <3 YOU! I knew it was something really small. Thanks again!

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    2,380
    You're welcome. If you didn't receive those warnings from your compiler, you should turn up the warning levels.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A little help here with long floats/scanf/printf
    By cprogrammer1980 in forum C Programming
    Replies: 6
    Last Post: 03-04-2011, 01:18 PM
  2. Segmentation fault on printf() [NOOB ALERT]
    By Richardcavell in forum C Programming
    Replies: 2
    Last Post: 02-26-2011, 05:44 PM
  3. Help on my C programming (noob)
    By gnozahs in forum C Programming
    Replies: 5
    Last Post: 09-17-2009, 07:48 AM
  4. Noob printf question
    By lolguy in forum C Programming
    Replies: 3
    Last Post: 12-14-2008, 07:08 PM
  5. Replies: 8
    Last Post: 11-29-2008, 10:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21