I'm trying to pass the valuee from the arrays for age, sex, and wTime to the cal_Fitness_Level function, but I can't seem to get it right. I either get the same level for everyone or garbage. code and data follows.
#include <fstream>
#include <string>
#include <iomanip>
#include <iostream>
using namespace std;
int cal_Fitness_Level(int age[], char sex[], int wTime[]);
void sortData(string lName[], string fName[], int age[], char sex[], int wTime[], int noOfRows);
int main ()
{
// step 1
string lName[15];
string fName[15];
int age[15], wTime[15], i = 0, num = 0;
int level[15];
char sex[15];
ifstream inFile; // input stream variable for data file
ofstream outFile; // output stream variable for result data
inFile.open("data.txt");
if (!inFile) // step 3
{
cout << "Cannot open the input file." << endl;
return 1;
}
outFile.open("results.txt");// step 4
outFile << setfill(' ') << left << setw(15) << "Last Name" //header for output file
<< setfill(' ') << left << setw(22) << "First Name"
<< setfill(' ') << left << setw(7) << "Age"
<< setfill(' ') << left << setw(7) << "Gender"
<< setfill(' ') << left << setw(10) << "Walk Time"
<< setfill(' ') << left << setw(13) << "Fitness Level" << endl;
while (!inFile.eof())
{
inFile >> fName[i] >> lName[i] >> age[i] >> sex[i] >> wTime[i]; // step 5
level[i] = cal_Fitness_Level(age, sex, wTime);// step 6
i++;
num++;
}
sortData(lName, fName, age, sex, wTime, i);// step 7
for (i = 0; i < 9; i++)// step 8
{
outFile << setfill(' ') << left << setw(15) << lName[i]
<< setfill(' ') << left << setw(22) << fName[i]
<< setfill(' ') << left << setw(9) << age[i]
<< setfill(' ') << left << setw(7) << sex[i]
<< setfill(' ') << left << setw(11) << wTime[i]
<< setfill(' ') << left << setw(7) << level << endl;
}
outFile << '\n' << "Number of records: " << num << endl;
return 0;
}
int cal_Fitness_Level(int age[], char sex[], int wTime[])
{
int i = 0, fL;
if (age[i] >= 13 && age[i] <= 19)
{
if (wTime[i] >= 48)
fL = 1;
else if (wTime[i] > 43 && wTime[i] <= 47)
fL = 2;
else if (wTime[i] > 39 && wTime[i] <= 43)
fL = 3;
else if (wTime[i] > 35 && wTime[i] <= 39)
fL = 4;
else if (wTime[i] < 35)
fL = 5;
}
else
fL = 0;
return fL;
}
void sortData(string lName[], string fName[], int age[], char sex[], int wTime[], int noOfRows)
{
int i, j;
int min;
// selection sort
for (i = 0; i < noOfRows - 1; i++)
{
// step a
min = i;
for (j = i + 1; j < noOfRows; j++)
if (lName[j] < lName[min])
min = j;
if(min!=i)// step b
lName[i].swap(lName[min]);
fName[i].swap(fName[min]);
age[i] = age[min];
sex[i] = sex[min];
wTime[i] = wTime[min];
}
}
Michael Brooks 13 M 33
Amy Shields 30 F 40
Clara Miles 50 F 30
Robert Davidson 20 M 45
Joshua Chase 25 M 42
Jackie Choker 20 F 29
Sarla Kothari 60 F 37
George Runner 53 M 49
Sally Jones 19 F 47