calculate sales tax

M

matt

I am trying to write a basic app to take a price and calculate the
sales tax and print the total. Here is the code I have.

#include <stdio.h>
#define STAX .0825

main(){

int price;
printf("What is the price?\n");
scanf("%d", price);
price += STAX;
printf("The sales tax is: %f\n", (float)price);
}

When I run it, it prompts for a price but says "Bus Error". I think it
may be the STAX but am new to c programming.
 
B

Brett Hoerner

matt said:
I am trying to write a basic app to take a price and calculate the
sales tax and print the total. Here is the code I have. ....
price += STAX;
printf("The sales tax is: %f\n", (float)price);
....

Unrelated to your question, but should this be price = price + (price *
STAX)?

You're just adding 8.25 cents to the final price.
 
C

Christopher Benson-Manica

matt said:
#include <stdio.h>
#define STAX .0825

main is best defined (and in the current standard, *must* be defined,
as returning an int.

int main( void ) { /* void is also a good idea */
int price;

Why are you using an int here? You later attempt to add .0825 to it,
which will accomplish nothing as the result will be truncated.
Declare price as a double.
printf("What is the price?\n");
scanf("%d", price);
price += STAX;
printf("The sales tax is: %f\n", (float)price);

This cast is not going to help you.
When I run it, it prompts for a price but says "Bus Error". I think it
may be the STAX but am new to c programming.

Walter already pointed out the actual error in your code.
 
B

Brett Hoerner

Brett said:
...

Unrelated to your question, but should this be price = price + (price *
STAX)?

Sorry, I was also confused by your variable name. The Price would be
what I said, but since you're using "price" to display the tax cost, it
would just be price = price * STAX

If I'm correct in understanding this, anyway.
 
M

Mike Wahler

matt said:
I am trying to write a basic app to take a price and calculate the
sales tax and print the total. Here is the code I have.

#include <stdio.h>
#define STAX .0825

main(){

int main() {
int price;
printf("What is the price?\n");
scanf("%d", price);

scanf("%d", &price);

Carefully read your documentation of 'scanf()'.
price += STAX;

This will not alter the value of 'price'.


printf("The sales tax is: %f\n", (float)price);

return 0; /* unless using C99 */
}

When I run it, it prompts for a price but says "Bus Error". I think it
may be the STAX but am new to c programming.

The incorrect use of 'scanf()' is probably the cause of your
'bus error'. But I think you need to rethink what you're doing.

Don't you want to add 8.25% to the price? That would involve
at least a multiplication. Also, note that although multiplying
a floating point value by an integer will indeed give a floating
point result, assigning that result to an integer type will
discard any fractional portion.

int price = 0;
double result = 0 /* [1] */
/* get price */

result = price + (price * STAX);

/* or you could redefine STAX to 1.0825 and just multiply: */

result = price * STAX;

[1] You should prefer 'double' over 'float' unless you
have a compelling reason to do otherwise.

-Mike
 
M

Mike Wahler

Brett Hoerner said:
...

Unrelated to your question, but should this be price = price + (price *
STAX)?

You're just adding 8.25 cents to the final price.

He's adding zero.

-Mike
 
M

Mike Wahler

Mike Wahler said:
int price = 0;
double result = 0 /* [1] */
/* get price */

result = price + (price * STAX);

Oops sorry I read too quickly and assumed you were computing
a total, not just the tax.

result = price * STAX;

But 'result' is no longer needed:

printf("%f\n", price * STAX);

Sorry for the mistake.

-Mike
 
M

matt

After a lot of playing around here is what I not have:

#include <stdio.h>
#define STAX .0825

int main(){
double price;
printf("What is the price?\n");
scanf("%f", &price);
printf("%f\n", (double)price);
price = price * STAX;
printf("The sales tax is: %.2f\n", price);
return 0;
}

When prompted for a price I put in 9.99 and here is what is returned:

What is the price?
9.99
521666.640505
The sales tax is: 43037.50

Not exactly what I expected.
 
N

Netocrat

On Thu, 03 Nov 2005 18:19:51 -0800, matt wrote:

[...]
double price;
printf("What is the price?\n");
scanf("%f", &price);
^^

You need %lf here. The format code is different to printf where you've
correctly used %f for a double.
printf("%f\n", (double)price);

[...]
 
K

Keith Thompson

matt said:
After a lot of playing around here is what I not have:

#include <stdio.h>
#define STAX .0825

int main(){
double price;
printf("What is the price?\n");
scanf("%f", &price);

"%f" expects a pointer to float. For a pointer to double, use "%lf".
(Yes, this is an asymmetry with respect to printf.)
printf("%f\n", (double)price);
price = price * STAX;
printf("The sales tax is: %.2f\n", price);
return 0;
}

Even for a small program like this, indentation would make your code
easier to read.

For style points, change "int main()" to "int main(void)". Both forms
are acceptable, but the latter is preferred.
 
M

matt

double price;
^^

You need %lf here. The format code is different to printf where you've
correctly used %f for a double.

#include <stdio.h>
#define STAX .0825

int main(){
double price;
printf("What is the price?\n");
scanf("%lf", &price);
printf("%.2f\n", (double)price);
price = (price + (price * STAX));
printf("The sales tax is: %.2f\n", price);
return 0;
}

What is the price?
100
100.00
The sales tax is: 108.25

Thanks to all!!!
 
M

Mike Wahler

matt said:
After a lot of playing around here is what I not have:

#include <stdio.h>
#define STAX .0825

int main(){
double price;
printf("What is the price?\n");
scanf("%f", &price);

sscanf("%lf", &price);

Previously I wrote:
"Carefully read your documentation of 'scanf()'."

Emphasize 'carefully'. :)

-Mike
 
W

W H G

matt said:
After a lot of playing around here is what I not have:

#include <stdio.h>
#define STAX .0825

int main(){
double price;
printf("What is the price?\n");
scanf("%f", &price); // try %lf , price is double,
// %f is for float
printf("%f\n", (double)price); // (double) not needed: price is double
price = price * STAX;
printf("The sales tax is: %.2f\n", price);
return 0;
}

When prompted for a price I put in 9.99 and here is what is returned:

What is the price?
9.99
521666.640505
notice != 9.99, scanf error so I checked
that %lf is needed.
The sales tax is: 43037.50
this is .0825 * 521666.640505 so calculation ok
Not exactly what I expected.

- G
 
R

Randy Howard

matt wrote
(in article
#include <stdio.h>
#define STAX .0825

int main(){

You might as well make that int main(void) to indicate you
aren't doing anything with command line arguments.
double price;
printf("What is the price?\n");
A bit more user friendly, try dropping the \n above, adding a
space or two and adding a call to fflush(stdout);
scanf("%f", &price);

scanf and printf format specifiers are not identical. Try %lf
here and watch what happens. Read your docs on scanf() again.
Then decide whether or not you want to figure out a better way
to get input than scanf(). :)

Also, if you are using gcc, turn up the warnings, something lik
gcc -Wall -O2 -pedantic
might be a good place to start.
printf("%f\n", (double)price);

You want the result to be a float, not a double with the %f to
printf.
 
M

Mike Wahler

Randy Howard said:
matt wrote
(in article


You might as well make that int main(void) to indicate you
aren't doing anything with command line arguments.

A bit more user friendly, try dropping the \n above, adding a
space or two and adding a call to fflush(stdout);

scanf and printf format specifiers are not identical. Try %lf
here and watch what happens. Read your docs on scanf() again.
Then decide whether or not you want to figure out a better way
to get input than scanf(). :)

Also, if you are using gcc, turn up the warnings, something lik
gcc -Wall -O2 -pedantic
might be a good place to start.


You want the result to be a float, not a double with the %f to
printf.

No. The floating point value will be promoted to 'double' if
that's not already its type. For 'printf()', "%f" denotes
'double', not 'float'.


(The cast is superfluous, btw).

-Mike
 
J

Joe Wright

matt said:
After a lot of playing around here is what I not have:

#include <stdio.h>
#define STAX .0825

int main(){
double price;
printf("What is the price?\n");
scanf("%f", &price);
printf("%f\n", (double)price);
price = price * STAX;
printf("The sales tax is: %.2f\n", price);
return 0;
}

When prompted for a price I put in 9.99 and here is what is returned:

What is the price?
9.99
521666.640505
The sales tax is: 43037.50

Not exactly what I expected.
You need more help than you are getting here. There are so many things
you don't seem to understand. Please examine this code..

#include <stdio.h>
#include <stdlib.h>

#define STAX 0.0825

int main(void) {
double price;
char cin[20]; /* arbitrary 'enough' length */
fputs("What is the price? ", stdout);
fflush(stdout);
fgets(cin, sizeof cin, stdin);
price = strtod(cin, NULL);

printf("Price %.2f\n", price);
printf("Rate %.4f\n", STAX);
printf("Tax %.2f\n", price * STAX);
printf("Total %.2f\n", price * (1 + STAX));

return 0;
}

I did not comment it because I hope you will try to read and understand it.
 
M

Mike Wahler

Joe Wright said:
matt said:
After a lot of playing around here is what I not have:

#include <stdio.h>
#define STAX .0825

int main(){
double price;
printf("What is the price?\n");
scanf("%f", &price);
printf("%f\n", (double)price);
price = price * STAX;
printf("The sales tax is: %.2f\n", price);
return 0;
}

When prompted for a price I put in 9.99 and here is what is returned:

What is the price?
9.99
521666.640505
The sales tax is: 43037.50

Not exactly what I expected.
You need more help than you are getting here. There are so many things you
don't seem to understand. Please examine this code..

#include <stdio.h>
#include <stdlib.h>

#define STAX 0.0825

int main(void) {
double price;
char cin[20]; /* arbitrary 'enough' length */
fputs("What is the price? ", stdout);
fflush(stdout);
fgets(cin, sizeof cin, stdin);
price = strtod(cin, NULL);

printf("Price %.2f\n", price);
printf("Rate %.4f\n", STAX);
printf("Tax %.2f\n", price * STAX);
printf("Total %.2f\n", price * (1 + STAX));

return 0;
}

I did not comment it because I hope you will try to read and understand
it.

While I don't disagree that this is a better approach to
the input, it doesn't help OP understand why his use
of 'scanf()' was wrong to simply tell him to use
something else. It also doesn't explain why assigning
a floating point value to an integer type loses data.

-Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top