The code below is designed as a vending machine code for a movie theater lobby. Everything works just fine except for the decimals in the currency. instead of displaying 5.00 or 7.00 for currency in proper double form it cuts even dollar values off to either 5 or 7. if its not an even dollar amount it displays correctly like with 2.25, 6.75, or 4.50. It does the same thing with returning change. even amounts drop the .00 off the end. How can i fix this? Would it be through the setprecision function?
This has nothing to do with the question, but you should change your variables and constants to floats. Your conversions don't need to be that accurate. Let your program use less memory. I can check out this program later, because I only have like 5 minutes of study hall left.
#include <iostream>
#include <iomanip>
double value[] =
{
5.00,
2.25,
7.00,
6.75,
4.50,
20.00
};
constunsigned valueElements = sizeof(value) / sizeof(value[0]) ;
int main()
{
std::cout << std::fixed ; // precision now affects the number of digits to show
std::cout << std::setprecision(2) ; // after the decimal point.
for ( unsigned i=0; i<valueElements; ++i )
std::cout << '$' << std::setw(6) << value[i] << '\n' ;
}
Fredbill30 wrote:
This has nothing to do with the question, but you should change your variables and constants to floats.
No, he shouldn't.
Your conversions don't need to be that accurate. Let your program use less memory.
Since it will have absolutely no effect on how much memory his program uses, I'd stick with double. And if it did, I'd still stick with double unless and until it became an issue.
It has an affect on how much RAM it uses. That's what I meant by "memory".
No, it doesn't. If all variables are allocated on the stack and the stack has a fixed size, there is no way for those variables to affect the amount of memory the program uses.