| | |
Passing a Value between an Array and Another Function
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Apr 2006
Posts: 9
Reputation:
Solved Threads: 0
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.
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
C++ Syntax (Toggle Plain Text)
#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
•
•
Join Date: Apr 2006
Posts: 9
Reputation:
Solved Threads: 0
•
•
•
•
Originally Posted by Salem
Try
C++ Syntax (Toggle Plain Text)
level[i] = cal_Fitness_Level(age[i], sex[i], wTime[i]);
•
•
Join Date: Apr 2006
Posts: 1
Reputation:
Solved Threads: 0
•
•
•
•
Originally Posted by Saint48198
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.
C++ Syntax (Toggle Plain Text)
int cal_fitness_level(int age, int sex, int wTime);
and call it like this:
C++ Syntax (Toggle Plain Text)
level[i] = cal_fitness_level(age[i], sex[i], wTime[i]);
> 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.
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.
![]() |
Similar Threads
- c language problm, how to pass pointer to a function (C)
- get length of a dynamic array (C++)
- Array (C++)
- passing the recordset data into an Array (ASP)
- Having trouble passing bool type to function....need help (C++)
- Need help passing a multi-dimensional array (C++)
- Function[Array] in combination with cin>> (C++)
Other Threads in the C++ Forum
- Previous Thread: werid can someone explain this
- Next Thread: MFC vs WIN forms - Which road to take
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays assignment beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data database delete desktop developer directshow dll encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelper iamthwee ifstream input int integer java lazy lib linux loop looping loops map math matrix memory multidimensional newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort string strings struct studio system template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






