Decimal (Double) Problem

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?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include <iostream>
#include <iomanip>
using namespace std;
const int CUSTOMER = 100;
const int SHUTDOWN = 9110;
const double beverage = 5.00;
const double candy = 2.25;
const double hotdog = 7.00;
const double popcorn = 6.75;
const double nachos = 4.50;
char getMenu (void);
double getSelection (char);
void getTendered (double);
int getPin (void);
int main()
{
char ch;
int pin, counter=0;
double cost;
do
{
system("cls");
pin = getPin();
if (pin==SHUTDOWN) { break; } 
ch = getMenu();
if (ch=='Q') { continue; }
counter++;
cost = getSelection(ch);
getTendered(cost);
system("pause");
} 
while (counter!= CUSTOMER);
cout<<endl;
cout<<"This unit is shutting down to restock"<<endl<<endl;
system("pause");
return 0;
}
char getMenu (void)
{
char ch;
int counter;
do
{
ch = '\n';
system("cls");
cout<<"Please select a Snack\n"
<<"B - Beverage $5.00\n"
<<"C - Candy $2.25\n"
<<"H - Hot Dog $7.00\n"
<<"N - Nachos $4.50\n"
<<"P - Popcorn $6.75\n"
<<"Q - Quit"<<endl;
cin>>ch; 
ch = toupper(ch);
}
while (ch!='B' && ch!='C' && ch!='H' && ch!='N' && ch!='P' && ch!='Q');
return ch;
}
double getSelection (char ch)
{
double cost;
cout<<"\nYou selected ";
switch(ch)
{
case 'B': cout<<"Beverage"<<endl; cost = beverage; break;
case 'C': cout<<"Candy"<<endl; cost = candy; break;
case 'H': cout<<"Hot Dog"<<endl; cost = hotdog; break;
case 'N': cout<<"Nachos"<<endl; cost = nachos; break;
case 'P': cout<<"Popcorn"<<endl; cost = popcorn; break;
}
return cost;   
}
void getTendered (double cost)
{
double tendered=0, change = 0;
do
{
cout<<"Please insert "<<cost<<"\nEnter amount tendered ";
cin>>tendered;
cin.clear();
cin.ignore(10,'\n');
cost = cost - tendered;
}
while (cost>0);
cost = (-1 * cost);
if (cost>0) 
{ 
cout<<"Change: "<<cost<<endl; 
}
}
int getPin(void)
{
int pin;
do
{
system("cls");
cout<<"Welcome to the Movie Food System\n";
cout<<endl;
cout<<"Please enter your 4-digit pin code: ";
cin>>pin;
cin.clear();
cin.ignore(10,'\n');
}
while (pin<1000 || pin>9999);
return pin;
} 
closed account (N36fSL3A)
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <iomanip>

double value[] =
{
    5.00,
    2.25,
    7.00,
    6.75,
    4.50,
    20.00
};

const unsigned 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.
Last edited on
Thank you very much, that adjustment took care of it. :)
closed account (N36fSL3A)
It has an affect on how much RAM it uses. That's what I meant by "memory".
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.
Topic archived. No new replies allowed.