counting values

I'm writing a program that reads a list of sales prices and then outputs them along with the highest and second highest sales along with how many times they occurred.

I am having trouble with finding and counting the highest and second highest sales. I am in a beginner course so I cannot use anything too advanced. Also, the file should be read through only once.
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
#include <iostream> 
#include <iomanip> 
#include <fstream> 
using namespace std;  
int main() { 	
int ID; 	
double SALES; 	
double max; 	
double sec_max; 	
int max_count = 0; 	
int sec_max_count = 0;   	
cout << fixed << showpoint << setprecision(2); 	 	
ifstream infile; 	
infile.open("sales1.txt");  	
if (!infile) 	
{ 		
cerr << "File error.\n"; 		
return 1; 	
}  	
cout << setw(22) << "Acme Sales Report\n"; 	
cout << setw(5) << "ID" << setw(18) << "Sales\n\n";  	
infile >> ID >> SALES; 	
max = sec_max = SALES;  	
if (!infile) 	
{ 		
cerr << "Unable to read file.\n";
 return 2;
 	}  	
while (infile) 	
{  	
cout << setw(6) << ID << setw(10) << "$" << SALES << endl;  	if (max < SALES) 
	{ 		
max_count = 1; 		
max = SALES;				 	
} 	
if (max = SALES) 	
{ 		
max_count++; 	
} 	
if (sec_max < SALES && max > SALES) 	
{ 		
sec_max = SALES; 		
sec_max_count = 1;		 	
} 	
if (sec_max = SALES) 	
{ 		
sec_max_count++; 	
}  	
infile >> ID >> SALES;  	
if (infile.eof()) 	
{ 		
break; 	
} 	
}  	
cout << "High sales amount: $" << max << " occurs: " << max_count << " times.\n"; 	
cout << "Second highest sales amount: $" << sec_max << " occurs: " << sec_max_count << " times.\n";  	
return 0; }


this is the file being used
first column is just an arbitrary id
second column is the sale

1000 1000
1111 500
1500 1000
2000 900
2222 2000
2500 2000
3000 1500
3333 2000
3500 2000
4000 600
4444 2500
4500 2000
5000 2500
5500 2222
5555 2000
6000 1999
6500 2222
6666 2500
7000 100
7500 2100
7777 2150
I would correct you on the errors in comparison used at lines #36 and #45.
Use '==', which is the comparison operator. '=' is the assignment operator, which assigns the right hand side value to the left hand side variable.

For a detailed discussion on finding the maximum and second maximum value, refer to the post http://cplusplus.com/forum/beginner/25042/

There are simple and not so simple ways to find the max and second max values.
By the way, try getting the habit of commenting on your program (use // or /* */ to do so). I also used to not do this, but it makes it a lot easier for others to read your program (or for yourself if you want to modify it after a year or so) :)
And also, to make it easier for yourself you should try to use tabs (which I think are done automaticaly by a lot of programs)

so then you'd get:

1
2
3
4
5
6
7
// code code code
if (!infile) 	
{ 		
       cerr << "File error.\n"; 		
       return 1; 	
}  
//code code code 

Topic archived. No new replies allowed.