I am trying to learn c++ from a textbook (as so many others) but am having a really hard time following this example:

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. Print the results like this:

Acme Sales Report
ID Sales
9999 $9999.00
High sales amount: $9999.00 occurs: 99 times.
Second highest sales amount: $9999.00 occurs: 99 times.

I have three text files to use to get the information (sales1.txt, sales2.txt, sales3.txt) all with differing amounts.

So I have my code written here:

//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("sales3.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;
	}

And the code is successful at displaying the numbers, and selecting the largest and second largest number. However, the counter is always incorrect no matter what I do, add, rearrange, what have you. For example, it returns this:

Acme Sales Report

ID Sales

1000 $1000.00
1111 $1000.00
1500 $1000.00
2000 $1200.00
2222 $1200.00
2500 $1500.00
3000 $1500.00
3333 $1500.00
3500 $1000.00
4000 $1500.00
4444 $1500.00
4500 $2000.00
5000 $4000.00
5500 $2000.00
5555 $4000.00
6000 $2000.00
6500 $2000.00
6666 $2000.00
7000 $100.00
7500 $3000.00
7777 $4000.00

Highest sales amount: $4000.00 occurs: 5 times.
Second highest sales amount: $3000.00 occurs: 3 times.
Press any key to continue . . .

The numbers are correct- $4000 being highest and $3000 being 2nd highest, but they occur 3 times and 1 time respectively. I have the same problem no matter which .txt file I use, but here are the three sets of numbers if it helps any. I have no idea where my code is incorrect, any direction would be REALLY appreciated.

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


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

The problem is that when you find a new max or 2nd max you need to reset the counter back to 1.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.