Set precision isn't working...why?

Feb 18, 2011 at 9:28pm
My set precision doesn't seem to be doing anything. I'm following an example I found here on the Reference section. I'm dealing with money so instead of 0.8 outputting it should be 0.80.

Here's my code, see anything wrong?
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include <iostream>
#include <string>
#include <iomanip>
using namespace std; 


const int SIZE = 10;
struct Drink {
    string Name;
    double Cost;
    int numDrinks;
};

void fillMachine (Drink machine[], int &numitems);
void getChoice (Drink machine[], int numitems, int &i, int &choice);
void transaction (Drink machine[], int &numitems, int &i, int &choice);

int main()
{
    int i = 0;
    int numitems = 0;
    int choice = 0;
    Drink machine[SIZE];

	
	fillMachine (machine, numitems);
	getChoice (machine, numitems, i, choice);
	transaction (machine, numitems, i, choice);
   
    
    return 0;
}


    
void fillMachine (Drink machine[], int &numitems)
{
    string mydrink;
    // Get the drink name
   cout << "Enter the first drink, Q to quit: ";
   getline(cin, mydrink);
   
   while (mydrink != "Q" && numitems < SIZE)
   {
	   machine[numitems].Name = mydrink;
	   // Get the price
	   cout << "Enter the price: ";
	   cin >> machine[numitems].Cost;

	   // Get the quantity
	   cout << "Enter the quantity: ";
	   cin >> machine[numitems].numDrinks;
	   cin.ignore();
	   numitems++;
	   cout << "Enter the next drink, Q to quit: ";
	   getline(cin, mydrink);
	}

}

void getChoice (Drink machine[], int numitems, int &i, int &choice)
{
cout << endl;
for (i=0;i<numitems;i++)
	cout << (i+1) << ") " << machine[i].Name  << setw(8) << setprecision (3) << machine[i].Cost << endl;
cout << (i+1) << ") Exit" << endl;

cout << endl << "Choose one: ";
cin >> choice;

if (choice > (numitems+1))
	cout << "Bad choice." << endl;

cout << endl;


}




void transaction (Drink machine[], int &numitems, int &i, int &choice)
{
double money;
double change;
double total;



while (choice != i +1)
{	
	i = choice - 1;	

if (machine[i].numDrinks < 1)
		cout << "This item is not in stock." << endl;

if (machine[i].numDrinks > 0 )
{	

	cout << "Enter an amount of money" << endl;
	cin >> money;
	while (money < machine[i].Cost)
		{
		cout << "That's not enough money" << endl;
		cin >> money;
		}

	while (money > 1)
	{
		cout << "Enter at least " << machine[i].Cost << " and no more than 1 dollar." << endl;
	cin >> money;
	}

	
	cout << endl << "Enjoy your beverage!" << endl << endl;


		change = money - machine[i].Cost;
		cout << "Change calculated: " << setprecision (3) << change << endl;
		money = money - change;
		total = money + total;
	machine[i].numDrinks = machine[i].numDrinks - 1;
	cout << "There are " << machine[i].numDrinks << " drinks of that type left" << endl;

}
	getChoice (machine, numitems, i, choice);

}
	cout << "Total earnings: $" << setprecision (3) << total << endl;
	


} 
Feb 18, 2011 at 9:33pm
If you check the reference again, you'll notice you need to use "fixed" as well, otherwise it doesn't attach 0s.
Feb 18, 2011 at 9:41pm
Makes sense, would I just cout << fixed; before every set precision line?
Feb 18, 2011 at 9:43pm
I believe so, yes.
Feb 18, 2011 at 11:10pm
You were right, thank you.
Feb 18, 2011 at 11:14pm
You only need to say fixed only once in the entire program but it has to be before before any setprecision line.
Feb 19, 2011 at 12:44am
The reason for your problem is because without the "fixed" keyword, the precision starts at the beginning of the number. With the "fixed" keyword, it starts after the decimal point. For example:

1
2
3
double x = 12.5364
cout << setprecision(2);
cout << "x = " << x << endl;
x = 12


Whereas...

1
2
3
4
double x = 12.5364;
cout << fixed << showpoint;
cout << setprecision(2);
cout << "x = " << x << endl;
x = 12.53
Last edited on Feb 19, 2011 at 12:46am
Topic archived. No new replies allowed.