Comparing ints within a while loop (maximum and minimum)

Currently I am working on a project for uni (so just tips please) about ordering desks. Currently I've been stuck on how to make it display the least expensive desk ordered and the most expensive desk ordered.

here's all of my code:
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
  #include <iostream>				//required for I/O 
#include <iomanip>				//to set specifications on fields
#include <stdio.h>
#include <cmath>
#include <string.h>
#include <iomanip>

/******************************************
*     pre-processor
******************************************/
#define  cAdult  9.00			// 
#define baseCost 200 //base cost of a desk
using namespace std;			//access to standard library

/****************************************
*          function prototypes
****************************************/


/*****************************************
*   main() - the function that executes
*****************************************/
string name; //customer name
double length; //desk's length
double width; //desk's width
char woodType; //desk's type of wood
int drawersNumber; //desk's number of drawers
double surfaceArea; //surface area of the desk
float surfaceCost; //cost dependent on the desk's surface area
float woodCost; //cost dependent on the desk's wood type cost
float drawerCost; //cost dependent on the desk's number of drawers
double total; //total cost of desk
double bigTotal; //total cost of all desks

char orderDesk; //decides whether to reset the loop

int totalM; //total number of Mahogany desks ordered
int totalO; //total number of Oak desks ordered
int totalP; //total number of Pine desks ordered
int otherTotal; //total number of unspecified wood desks ordered

int totalDesks; //total number of desks ordered
double averageCost; //average cost of all desks




int main()
{
	
	int deskCount; //counts number of desks
		//do	{
	//deskCount++;

	totalM = 0;
	totalO = 0;
	totalP = 0;
	otherTotal = 0;
	
	cout << "Want to order a desk? (y/n)\n";
	cin >> orderDesk;
	
	while(orderDesk == 'y')
	{
	
	
	
	
	
	cout << "What is your name?" << endl;
	fflush(stdin) ;
	getline(cin, name);
	cout << "What is the length of the desk?\n";
	cin >> length;
	cout << "What is the width of the desk?\n";
	cin >> width;
	cout << "What type of wood is it made of?\n";
	cout << "	m = mahogany\n";
	cout << "	o = oak\n";
	cout << "	p = pine\n";
	cin >> woodType;
	cout << "Lastly, how many drawers does it have?\n";
	cin >> drawersNumber;
	cout << endl;
	
	surfaceArea = length * width;
	drawerCost = drawersNumber * 30;
	
	//decisions
	if(surfaceArea > 750)
	{
		surfaceCost = 50;
	}
	else if(surfaceCost < 750)
	{
		surfaceCost = 0;
	}
	
	if(woodType == 'm')
	{
		woodCost = 150;
		totalM = totalM + 1;
	}
	else if(woodType == 'o')
	{
		woodCost = 125;
		totalO = totalO + 1;
	}
	else if(woodType == 'p')
	{
		woodCost = 100;
		totalP = totalP + 1;
	}
	else
	{
		woodCost = 0;
		otherTotal = otherTotal + 1;
	}
	
	//final equation
	total = baseCost + surfaceCost + drawerCost + woodCost;
	
	//output

	cout << "1	Customer name "<< setw(16) << name << endl;
	cout << "2	   Desk length" << setw(13) << length << endl;
	cout << "3	   Desk width" << setw(14) << width << endl;
	if(drawersNumber > 0)
	{
		cout << "4	   Number of drawers" << setw(7) << drawersNumber << endl;
	}
	
	if(woodType == 'm')
	{
		cout << "5	   Wood type" << setw(17) << "Mahogany\n";
	}
	else if(woodType == 'o')
	{
		cout << "5	   Wood type" << setw(17) << "Oak\n";
	}
	else if(woodType == 'p')
	{
		cout << "5	   Wood type" << setw(17) << "Pine\n";
	}
	else
	{
		cout << "5\n";
	}
	
	cout << "6\n";
	cout << "7	   cost estimate\n";
	cout << "8" << setw(20) << "$200.00" << setw(16) << "Base Cost\n";
	
	if(surfaceArea > 750)
	{
		cout << "9" << setw(19) << "$50.00" << setw(6) << "	   Surface area greater than 750 square inches\n";
	}
	else
	{
		cout << "9\n";
	}
	cout << setprecision(2) << fixed;
	cout << "10" << setw(13) << "$" << drawerCost << "	   Cost of drawers ($30 per drawer)" << endl;
	
	if(woodType == 'm')
	{
		cout << "11" << setw(19) << "$150.00" << setw(15) << "Mahogany\n";
	}
	else if(woodType == 'o')
	{
		cout << "11" << setw(19) << "$125.00" << setw(15) << "Oak\n";
	}
	else if(woodType == 'p')
	{
		cout << "11" << setw(19) << "$100.00" << setw(15) << "Pine\n";
	}
	else
	{
		cout << "11\n";
	}
	cout << "12" << setw(30) << "--------------------\n";
	cout << "13" << setw(10) << "$" << total << "	Total cost\n";
	bigTotal += total;
	
	cout << "Want to order a desk? (y/n)\n";
	cin >> orderDesk;
	}
	
	double mostExpensive = 200; //most expensive desk cost
	double leastExpensive = 201; //least expensive desk cost
	
	mostExpensive = (total > mostExpensive)?total:mostExpensive;
	leastExpensive = (total < leastExpensive)?total:leastExpensive;
	
	totalDesks = totalM + totalO + totalP + otherTotal;
	averageCost = bigTotal / totalDesks;
	
	if(orderDesk == 'n')
	{
		cout << "	*********** Wood Totals ***********\n";
		cout << setw(15) << totalM << " Mahogany desks\n";
		cout << setw(15) << totalO << " Oak desks\n";
		cout << setw(15) << totalP << " Pine desks\n";
		cout << setw(15) << otherTotal << " Other wood desks\n" << endl;
		cout << "	*********** Desk Totals ***********\n";
		cout << "	Total number of desks" << setw(15) << totalDesks << endl;
		cout << setprecision(2) << fixed;
		cout << "	Total costs of all desks" << setw(10) << "$ " << bigTotal << endl;
		cout << "	Average cost per desk   " << setw(10) << "$ " << averageCost << endl;
		cout << "	Least expensive desk    " << setw(10) << "$ " << leastExpensive << endl;
		cout << "	Most expensive desk     " << setw(10) << "$ " << mostExpensive << endl << endl;
		   
	}
	
	system("PAUSE");			//pause for viewing data
	return 0;
	
}


whew, that was a lot. Sorry if it's messy/unprofessional. Here's the part I'm stuck on in particular:

1
2
3
4
5
        double mostExpensive = 200; //most expensive desk cost
	double leastExpensive = 201; //least expensive desk cost
	
	mostExpensive = (total > mostExpensive)?total:mostExpensive;
	leastExpensive = (total < leastExpensive)?total:leastExpensive;

note: the smallest possible amount a desk can cost is $200. Anyone know how to do this?
Last edited on
As far as I can tell the line mostExpensive = (total > mostExpensive) ? total : mostExpenisve; should always end up giving you the most expensive desk bought. (If not, what does it give instead?)
The initial values are the most important here. If you are using the lowest possible price to initialize the most expensive desk, what should the initial value be to find the least expensive desk?

If that's too vague, I can tell more. I may be bad at giving tips.
Last edited on
Line 189/190 shows:

1
2
double mostExpensive = 200; //most expensive desk cost
double leastExpensive = 201; //least expensive desk cost 


^Seems kinda wrong. Most expensive is 1 less than the least expensive?



You should make the variables for checking these things earlier on so you can check for lowest/highest prices as you go. They may change several times as new desks are ordered, but they'll be ready when you need them.
Last edited on
You also shouldn't be initializing them inside the loop, over and over again!
They need to be defined before the loop.
You could init mostExpensive to 0 (or even a negative value) and leastExpensive to 1e9 (a billion, or more).
So for each desk I put in the same value to test the difference (since at one point I think only mostExpensive worked) except for the type of wood, giving me the 3 different outputs of 490, 465, and 440. It resulted in this:

        Least expensive desk            $ 201.00
        Most expensive desk             $ 440.00


So I gave a shot at changing the leastExpensive initial value to 99999 since the price can be infinite if you add enough drawers (and used the same values).

        Least expensive desk            $ 440.00
        Most expensive desk             $ 440.00


Not sure what's going on here. I understand what you mean, I think, but it's just not really working?
Did you move the definitions of leastExpensive and mostExpensive to before the loop? (Did you see my post just above yours?)
It's hard to not just give you the answer. But if you simply keep track of every $ value for each desk as they go. The first one will be the least and most expensive desk. The next desk will replace it in either least or most (or might be the same cost and no change), and so on.
> Comparing ints within a while loop
the thing is that your comparisons (lines 192, 193) are not within the loop, but completely outside it.
that should be obvious if you indent properly (just ask the ide to do it for you)


> whew, that was a lot.
indeed.
you have more than 100 lines for user input, that I don't care about because it's not relevant to your issue.*
may as well replace all that with
1
2
double value;
while(cin >> value)
and then find the biggest and smallest value


*change that to «I shouldn't need to care about»
if you are messing up computing the total, then, of course, the max and min will give you garbage; but you should have tested that part before trying to operate on it
or you may just use a simpler input to test max and min
Last edited on
Alright, got it fixed! Sorry for the late response, I've been really busy and mixed up with family stuff, but thank you all for the help! First I moved the variable declarations up above int main(), then I put the actual comparison inside the loop.
Topic archived. No new replies allowed.