Here's my code:
include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
int main(){
int ID;
double largest;
double secondLargest;
double sales;
int largestCount = 0;
int secondLargestCount = 0;
ifstream infile ("sales3.txt");
if (!infile){
cout << "Error: cannot find input data!\n";
return 1;
}
cout << " Acme Sales Report \n";
cout << " ID Sales \n";
cout << "-------- ----------\n";
infile >> ID >> sales;
largest = sales;
secondLargest = sales;
while (infile){
if(sales > largest){
largestCount++;
largest = sales;
}
if (sales < largest && sales > secondLargest) {
secondLargestCount++;
secondLargest = sales;
}
cout << setw(6) << ID << setw(14)<< fixed << setprecision(2)<< sales << endl;
infile >> ID >> sales;
}
cout << "The largest sale is: " << largest << " It occurs: "
<< largestCount << " times." << endl;
cout << "The second largest sale is: " << secondLargest << " It occurs: "
<< secondLargestCount << " times." << endl;
infile.close();
return 0;
}
The title was a bit misleading - I meant to say largest and second largest values - not maximum and minimum values. I know the code is wrong - it can correctly find the maximum values, but doesn't always find the second largest values from a data file, but I'm not sure how to count the occurences of each. I'm positive I need to add 2 more if statements to figure this out, but I'm not sure. Any help would be appreciated.