Basically this is suppose to read zipcodes from a file and sorts them in ascending order then calculates the total amount processed. I am getting output, but I think my problem is somewhere in my sort function. The program pretty much runs down to the last zipcode in the file and prints it out and the rest come out as just 0's. What am I doing wrong?
#include <algorithm>
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cstring>
#include <iomanip>
using namespace std;
const int L = 100;
struct City
{
int zip_code;
int count;
double percent;
};
City Ar[L];
int buildArray( City [] );
int calcPerc( City [], int);
void printArray( City [], int, int);
void sortArray( City [], int );
int main()
{
int num;
int totalsales;
num = buildArray(Ar);
totalsales = calcPerc(Ar, num);
printArray(Ar, num, totalsales);
sortArray(Ar, num);
return 0;
}
int buildArray( City Ar[] )
{
ifstream inFile;
int temp = 0, number = 0, i;
inFile.open("zipcodes.txt");
inFile >> temp;
while(inFile)
{
for(i = 0; i < L; i++)
{
if(temp == Ar[i].zip_code){
Ar[i].count++;
break;}
Ar[i].zip_code = temp;
Ar[i].count = 1;
break;
}
inFile >> temp;
number++;
}
inFile.close();
return number;
}
int calcPerc( City Ar[], int num)
{
int i;
int totalsales;
for(i=0; i < L; i++)
totalsales = totalsales + Ar[i].count;
for(i=0; i < L; i++)
Ar[i].percent = (Ar[i].count / totalsales) * 100;
return totalsales;
}
void printArray( City Ar[], int num, int totalsales )
{
int i;
cout << fixed << setprecision(2) << showpoint;
cout << setw(15) << "Zip Code" << setw(15) << "Count" << setw(15) << "Percent" << endl;
for(i = 0; i < L; i++)
cout << setw(15) << Ar[i].zip_code << setw(15) << Ar[i].count
<< setw(15) << Ar[i].percent << endl;
cout << "Total Sales Processed: " << totalsales << endl;
}
void sortArray( City Ar[], int num )
{
int i;
int min;
int j;
for(i = 0; i < L; i++)
{
min = i;
for(j = i+1; j < L; j++)
{
if(Ar[j].zip_code < Ar[min].zip_code)
min = j;
}
swap(Ar[i], Ar[min]);
}
}