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.
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: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; }
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!