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

Try

level[i] = cal_Fitness_Level(age[i], sex[i], wTime[i]);

I get a compiler error (C2662) when I try that. I do not think that you can reference an array in that format when calling a function.

Of course you do.

Biut it makes no sense to pass the whole array to the function each time, when you're only reading into the array one element at a time.

I get a compiler error (C2662) when I try that. I do not think that you can reference an array in that format when calling a function.

If you are only interested in a single value inside each of the arrays, passing a pointer to the array makes no sense, instead, change your function declaration to be:

int cal_fitness_level(int age, int sex, int wTime);

and call it like this:

level[i] = cal_fitness_level(age[i], sex[i], wTime[i]);

That somewhat worked. The last three employees data is out of order. I've include the level variable in the sort function and modified the cal_fitness_level to look like Cup of Crazy's suggestion. I'm not sure why the last three are getting mixed up.

> I'm not sure why the last three are getting mixed up.
Is the data OK before you sort it?

> if(min!=i)
Consider using a lot more { } in this function, because it seems to me that not everything is being run at the correct time. Yes, braces can be optional in some circumstances, but if you take the approach of always using them, you minimise your surprises later on.

This article has been dead for over six months. Start a new discussion instead.