// Andrew Driscoll
#include <iostream.h>
int main ()
{
int rcup, ctr=0;
float price, gross=1, subtot, total;
char first[10], date[10];
cout << "First Name: "; cin >> first; cout << "\n";
cout << "(01/01/1001) Date: "; cin >> date; cout << "\n";
cout << "How many Reeses Cups are you buying?\n"; cin >> rcup;
while (ctr<rcup)
{
cout << "What is the price of the Reeses Cups?\n"; cin >> price;
gross=gross+price;
ctr++;
}
subtot=gross*.06; total=subtot+gross;
cout << "\n"; cout << "\n"; cout << "\n";
cout << "Cashier: " << first << "\n"; cout << "Date: " << date << "\n";
cout << "Tax: " << subtot << "\n"; cout << "Subtotal: " << gross << "\n"; cout << "Total: " << total << "\n";
cout << "Have a nice day!\n";
return 0;
}
/*
First Name: Andrew
(01/01/1001) Date: 07/02/9999
How many Reeses Cups are you buying?
2
What is the price of the Reeses Cups?
1.19
What is the price of the Reeses Cups?
2.18
Cashier: Andrew
Date: 07/02/9999
Tax: 0.2622 <-- Only want first two decimals.
Subtotal: 4.37
Total: 4.6322 <-- Only want first two decimals.
Have a nice day!
Press any key to continue
*/
at the top of the file. I'm actually somewhat surprised that your compiler is not complaining about the cout statements, since you haven't specified it, and your not clarifying it with std::cout. Dev-C++ complains that <iostream.h> is an antiquated header, too. What compiler are you using?
Sorry I didn't have time to give a more detailed explanation the last time. Here is an some example code (note that it has limitations. e.g. it would print 5.05 as 5.5 and the maximum value is about 40 million)
how about using printf??
use it in the way
printf("%1.2f", number);
this would give you the number with atleast 1 digit before decimal and exactly 2 digits after decimal....
It would, but floating point values are not really the right tool for representing currency. They are great for continuos values representing physical things like length, speed, weight etc. But currency is really a discrete value, and should be represented by a discrete data type.
You never want an amount of money to be 0.00000000000001 or 1.5e49.
There is a real danger with using floats for currency:
If you have something like 100,500.50, and add a large number (like a billion, I don't know exactly), precision information WILL be lost. You will probably end up with something like 1.00001e9. This equals one 1 billion + 100,000. In other words, you lost 500.50.