{
float price,cost,discount;
discount=(0.5);
cost =(discount)*price;
printf("please input the price of the item\n please do not use '$' in your reply.. \n thankyou\n");
scanf("%d",&price);
printf("your cost is %d",cost);
system("pause");
}
why won't this program calculate the correct cost..?.. I'm confused. when i run it,it keeps showing the wrong cost result every time! what's wrong with it?
Use code tags [ code ] and [ /code ] (no space) around your text, so one can point to the correct line
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include<stdio.h>
#include<stdlib.h>
main()
{
float price,cost,discount;
discount=(0.5);
cost =(discount)*price;
printf("please input the price of the item\n please do not use '$' in your reply.. \n thankyou\n");
scanf("%d",&price);
printf("your cost is %d",cost);
system("pause");
}
You declare an initialize discount, and you declare cost and price. On line 10, you try to calculate cost, but price is not initialized until line 12. All you need is to move line 10 after line 12
- This is a C not a C++ code.
- main must be declared as int function.
- price & discount are declared as float but scanf(line12) & printf(line 15) have the format for int.
Thankyou ats15 and condor.
But I did everything you guys told me .. and my program is still giving me wrong results....................... how comes...................................... "confused"
program:
#include<stdio.h>
#include<stdlib.h>
int main()
{
int price, cost,discount;
printf("please enter the price of the item\n please do not inclde: '$' in your response\n thankyou\n");
scanf("%d",&price);
discount = 0.5;
printf("your cost is %d\n thankyou for shopping at my store\n GOODBYE\n");
cost= (price)*discount;
Im just learning programming this year. This code looks a bit different then I have been using but......
If you are going to use decimals you should use doubles, int does not work with decimals. When you are performing your calculation... Lets say someone inputs 10.... the calculation, because its an int, would end up being 10 * 0 instead of 10 * 0.5. You could also use float instead of double.
The next thing I would suggest is to declare discount as a const.
const double discount = 0.5
Put that above your main. That way the discount never gets changed by accident and if you want to change it, its very easy.
Lastly, your calculation works fine because its a 50% discount. Exactly half. If the discount was anything else you would still get the wrong amount. There are a few ways you could do the math but the way I would do the calculation is this...
cost = price - (price * discount);
That multiples the price by the percent discount and then subtracts the money saved from the price... leaving you with the cost after discount.
If you used a 40% discount or .40, your displayed cost would have been 40 instead of the 60 it should be
Here's the modified code as I said before. Please try to understand and then modify it as you wish:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include <stdio.h>
#include <stdlib.h>
int main()
{
float price, cost, discount;
discount = 0.5;
printf("please input the price of the item\n");
printf("please do not use '$' in your reply..\nthankyou\n");
scanf("%f", &price);
cost = discount * price;
printf("your cost is %5.2f", cost);
system("pause");
return 0;
}
please input the price of the item
please do not use '$' in your reply..
thankyou
50
your cost is 25.00
Thank you scott7975!.. yes.. what you said was very helpful.. and did you say that you are just starting?.. could you please email me the notes that you get on a regular bases! thanks.(shanty_101@hotmail.com)
condor:
It worked!! thankyou!!!! But I have one question!
printf("your cost is %5.2f", cost);
why did you include the "5.2" in the printf statement?
thanks!. just checked it out. that web page really gives a lot of info.. but I don't really understand it yet though.. I didnt see them include 5.2..will read it over though...thanks alot.