Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C (http://www.programmingforums.org/forum60.html)
-   -   small problem (http://www.programmingforums.org/showthread.php?t=4030)

Navid May 21st, 2005 8:12 PM

small problem
 
:

#include <stdio.h>

float tax = .15;
int price;

int main(void)
{
printf("What is the Cost?");
scanf("%d", &price);

#define total price*tax+price

printf("\nTotal cost= %ld\n",total);
system("pause");
return 0;
}


What it does is it asks for the cost of something, then gives you the cost after adding tax to it. But something is wrong with my variable "tax". Can anyone help please?

Mjordan2nd May 21st, 2005 8:27 PM

I don't think it's a problem with tax. When you make total a variable it works fine. My guess is that there is a different way of defining that, but I don't know how.

nez May 21st, 2005 8:41 PM

:

#include <stdio.h>

flaot tax;
int price;
float total;

int main()
{
tax=0.15;
printf("What is the Cost?");
scanf("%d", &price);

total=(price*tax)+price;

printf("\nTotal cost = %2.2lf\n",total);

return 0;
}


I think that does the same thing as what u were trying to do but without the define bit, calling total a flaot instead

is this any help?
(i found it useful for me even if it didnt help you)
:D
nez

Navid May 21st, 2005 10:55 PM

yeah thanks man

but now i have to find out about "%2.2lf"

but this is my first day of C so blah :p

Mjordan2nd May 21st, 2005 11:15 PM

It's just going to print two decimal places with that.

nez May 22nd, 2005 6:12 AM

you could have just put "%lf" in but it wil print a number with 4 decimal places, the 2.2 just tames it a bit:

2.2 prints a number with 2 long to 2 decimal places i.e 10.65
4.3 prints a number with 4 long to 3 decimal places i.e 4512.854

hope this helps
:D
nez

Ooble May 22nd, 2005 7:54 AM

Basically, tax needs to equal 0.15, not .15.

Navid May 22nd, 2005 8:23 PM

thanks guys

even though SAMS has a good reputation, i always have questions

this forum shuting down would be a nightmare


All times are GMT -5. The time now is 12:59 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC