Trouble finding 2nd maximum number while using counter- help please!

I am a beginner programmer taking my first C++ class. I've had great luck so far just following examples/book information/instructor information/the occasional Google search. Tonight all my methods failed. Here's my assignment:

Write a program to input and display a sales file. Each input line contains an integer ID number and a sales amount. When the end of the file occurs, print the high sales amount and the number of times that amount occurred. Print also the second highest sales amount and the number of times that one occurred.

I have managed to get the program to display correctly, as well as find the largest and second largest numbers. However, no matter what I change (have spent upwards of 6 hours on this already, and am exhausted) the counter for finding the second largest number fails. If someone could point me in the right direction, I'd really appreciate it.

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
//Program: 
//Author: 
//Date: 9/26/11
//Description: Displays sales amounts and lists highest and second highest sales amounts and frequency.

#include <iostream>

#include <iomanip>

#include <fstream>

using namespace std;

int main()
{
	int id = 0;

	double sales = 0.0;

	int count1 = 0;

	int count2 = 0;

	double hiSales1 = 0.0;

	double hiSales2 = 0.0;
 
    ifstream inSales;

	inSales.open("sales1.txt", ios::in);
 
    if (!inSales)
    {
        cout<<"Cannot open file." << endl;

        return -1;
    }
 
    cout<< "Acme Sales Report" << endl << endl;

	cout<< "ID        Sales" << endl << endl;
	 
    inSales >> id;
	
	inSales.ignore(1);
	
	inSales >> sales;

	    while(!inSales.eof())
	    {
         if(sales > hiSales1)
        {

		 hiSales1 = sales;

         count1++;
		}		 
		 if(sales < hiSales1 && sales > hiSales2)
		{			
		hiSales2 = sales; 
 
		 count2++;
	    }

        cout << fixed << setprecision(2);
	    
		cout << id << setw(7) << "$" << sales << endl; 
	     
		inSales >> id;
		
		inSales.ignore(1);
		
		inSales >> sales;	      
	    }

		cout << endl << "Highest sales amount: $" << hiSales1 << " occurs: " << count1 << " times.";

		cout << endl << "Second highest sales amount: $" << hiSales2 << " occurs: " << count2 << " times." << endl;

	    inSales.close ();

	    return 0;
	}


I didn't tag much in the code because it seems pretty self explanatory. If anything is confusing let me know and I'll try to explain better.
Last edited on
Could you show the contents of sales1.txt so we can test it?

I'm guessing it's just due to the order in which the items come. Imagine this list:
1 2 3 4 5 6 7 8 9 10
-> count1 will be 10, because a new maximum is found at every step. count2 will be 0.

The thing is, every step is checked separately and sequentially. The 2nd highest number could be the maximum at the moment it is evaluated. Once a higher value is found, the number is discarded.

Maybe add an extra check in the if at line 51 to check whether the old hiSales1 is higher than hiSales2, and if so replace it before discarding the value.
This is the text from sales1.txt -

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

There were originally more spaces between the columns, so I reduced it down to 1 character between the two thinking that would solve the problem for my inSales.ignore(1); on line 71.

I could not get the additional check to make any difference. It returned dollar amounts as $0.00 instead, and the count was 0. I played around with the location, but it didn't help.

This is a problem I just found- when I run the program with sales2.txt or sales3.txt, it doesn't even return the right amounts. I really don't understand what is flawed in my code that would cause this kind of massive variation between the txt files. Here are the other two for relevancy:

sales2.txt

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 3000
7777 2150



sales3.txt

1000 1000
1111 1000
1500 1000
2000 1200
2222 1200
2500 1500
3000 1500
3333 1500
3500 1000
4000 1500
4444 1500
4500 2000
5000 4000
5500 2000
5555 4000
6000 2000
6500 2000
6666 2000
7000 100
7500 3000
7777 4000



Thanks for the reply, any more ideas?

Last edited on
Topic archived. No new replies allowed.