Initialize your arrays to zero before you start incrementing.
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;
enum CarType
{
Ford,
Chevy,
Honda,
Toyota
};
struct CarCustomer
{
string firstName;
string lastName;
double price;
CarType carType;
};
void calcCarStats(CarCustomer arrCustomers[], int count, int carCount[])
{
for(int index = 0; index < count; index++)
{
carCount[arrCustomers[index].carType]++;
}
}
void displayCarTypeCounts(int carCount[])
{
for(int index = Ford; index <= Toyota; index++)
{
cout //<< translateCarTypeToText((CarType)index) << " "
<< carCount[index] << endl;
}
}
int main(int, char* [])
{
int count = 0;
CarCustomer arrCustomers[100]; //Array of structs for the Struct CarCustomer
int carCount[100] = {0};
double carPriceSum[100] = {0};
double carPriceAvg[100] = {0};
ifstream fin("input.dat");
if(!fin)
{
cout << "Error opening file, check the file name" << endl;
return -1;
}
while (true)
{
// Joe Smith 5999.99 0
int carTypeInt;
CarCustomer carCustomer;
fin >> carCustomer.firstName
>> carCustomer.lastName
>> carCustomer.price
>> carTypeInt;
carCustomer.carType = (CarType)carTypeInt; // carType entry is an enum
if (!fin) { break; }
arrCustomers[count++] = carCustomer;
}
fin.close();
calcCarStats(arrCustomers, count, carCount);
displayCarTypeCounts(carCount);
return 0;
}