help.. c++

#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");
}


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
OK, ats15.

@abriella1:

- 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.
Last edited on
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;

system("pause");
}
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

Hope that helps
Last edited on
@abriella1

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

I hope this time will help you.
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?
@abriella1

You're welcome!
A very good explanation you will find at:

http://www.cplusplus.com/reference/cstdio/printf/?kw=printf


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.
abriella1, see Example (at the bottom):

printf ("floats: %4.2f %+.0e %E \n", 3.1416, 3.1416, 3.1416);

Happy programming!
Oh!.. I get it now.. thankyou..lol
condor.. could you please reply to my next topic that i posted.. title:'question'
@abriella1

If you consider all OK here, please mark this thread as Solved.
what do you mean by that condor?
@abriella1

I understood from what you wrote before that now your program works.

It worked!! thankyou!!!!

So, this thread should be closed and marked as Solved. That's all!
o.. i marked it..:-)
#condor#
Topic archived. No new replies allowed.