944,208 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 3431
  • C++ RSS
Apr 12th, 2006
0

Passing a Value between an Array and Another Function

Expand Post »
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.

C++ Syntax (Toggle Plain Text)
  1. #include <fstream>
  2. #include <string>
  3. #include <iomanip>
  4. #include <iostream>
  5.  
  6. using namespace std;
  7.  
  8. int cal_Fitness_Level(int age[], char sex[], int wTime[]);
  9. void sortData(string lName[], string fName[], int age[], char sex[], int wTime[], int noOfRows);
  10.  
  11. int main ()
  12. {
  13. // step 1
  14. string lName[15];
  15. string fName[15];
  16. int age[15], wTime[15], i = 0, num = 0;
  17. int level[15];
  18. char sex[15];
  19. ifstream inFile; // input stream variable for data file
  20. ofstream outFile; // output stream variable for result data
  21.  
  22. inFile.open("data.txt");
  23.  
  24. if (!inFile) // step 3
  25. {
  26. cout << "Cannot open the input file." << endl;
  27. return 1;
  28. }
  29.  
  30. outFile.open("results.txt");// step 4
  31.  
  32. outFile << setfill(' ') << left << setw(15) << "Last Name" //header for output file
  33. << setfill(' ') << left << setw(22) << "First Name"
  34. << setfill(' ') << left << setw(7) << "Age"
  35. << setfill(' ') << left << setw(7) << "Gender"
  36. << setfill(' ') << left << setw(10) << "Walk Time"
  37. << setfill(' ') << left << setw(13) << "Fitness Level" << endl;
  38.  
  39. while (!inFile.eof())
  40. {
  41. inFile >> fName[i] >> lName[i] >> age[i] >> sex[i] >> wTime[i]; // step 5
  42. level[i] = cal_Fitness_Level(age, sex, wTime);// step 6
  43. i++;
  44. num++;
  45. }
  46.  
  47. sortData(lName, fName, age, sex, wTime, i);// step 7
  48.  
  49. for (i = 0; i < 9; i++)// step 8
  50. {
  51. outFile << setfill(' ') << left << setw(15) << lName[i]
  52. << setfill(' ') << left << setw(22) << fName[i]
  53. << setfill(' ') << left << setw(9) << age[i]
  54. << setfill(' ') << left << setw(7) << sex[i]
  55. << setfill(' ') << left << setw(11) << wTime[i]
  56. << setfill(' ') << left << setw(7) << level << endl;
  57. }
  58.  
  59. outFile << '\n' << "Number of records: " << num << endl;
  60.  
  61. return 0;
  62. }
  63.  
  64. int cal_Fitness_Level(int age[], char sex[], int wTime[])
  65. {
  66. int i = 0, fL;
  67.  
  68. if (age[i] >= 13 && age[i] <= 19)
  69. {
  70. if (wTime[i] >= 48)
  71. fL = 1;
  72. else if (wTime[i] > 43 && wTime[i] <= 47)
  73. fL = 2;
  74. else if (wTime[i] > 39 && wTime[i] <= 43)
  75. fL = 3;
  76. else if (wTime[i] > 35 && wTime[i] <= 39)
  77. fL = 4;
  78. else if (wTime[i] < 35)
  79. fL = 5;
  80. }
  81. else
  82. fL = 0;
  83.  
  84. return fL;
  85. }
  86.  
  87.  
  88. void sortData(string lName[], string fName[], int age[], char sex[], int wTime[], int noOfRows)
  89. {
  90. int i, j;
  91. int min;
  92.  
  93. // selection sort
  94. for (i = 0; i < noOfRows - 1; i++)
  95. {
  96. // step a
  97. min = i;
  98.  
  99. for (j = i + 1; j < noOfRows; j++)
  100. if (lName[j] < lName[min])
  101. min = j;
  102.  
  103. if(min!=i)// step b
  104. lName[i].swap(lName[min]);
  105. fName[i].swap(fName[min]);
  106. age[i] = age[min];
  107. sex[i] = sex[min];
  108. wTime[i] = wTime[min];
  109. }
  110. }

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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Saint48198 is offline Offline
9 posts
since Apr 2006
Apr 12th, 2006
0

Re: Passing a Value between an Array and Another Function

Try
C++ Syntax (Toggle Plain Text)
  1. level[i] = cal_Fitness_Level(age[i], sex[i], wTime[i]);
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Apr 12th, 2006
0

Re: Passing a Value between an Array and Another Function

Quote originally posted by Salem ...
Try
C++ Syntax (Toggle Plain Text)
  1. 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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Saint48198 is offline Offline
9 posts
since Apr 2006
Apr 12th, 2006
0

Re: Passing a Value between an Array and Another 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.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Apr 12th, 2006
0

Re: Passing a Value between an Array and Another Function

Quote 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.
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:

C++ Syntax (Toggle Plain Text)
  1. int cal_fitness_level(int age, int sex, int wTime);

and call it like this:

C++ Syntax (Toggle Plain Text)
  1. level[i] = cal_fitness_level(age[i], sex[i], wTime[i]);
Reputation Points: 10
Solved Threads: 0
Newbie Poster
CupOfCrazy is offline Offline
1 posts
since Apr 2006
Apr 12th, 2006
0

Re: Passing a Value between an Array and Another Function

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Saint48198 is offline Offline
9 posts
since Apr 2006
Apr 12th, 2006
0

Re: Passing a Value between an Array and Another Function

> 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.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Apr 12th, 2006
0

Re: Passing a Value between an Array and Another Function

I added the {} for if(min!=i) but there was no change
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Saint48198 is offline Offline
9 posts
since Apr 2006
Apr 13th, 2006
0

Re: Passing a Value between an Array and Another Function

> Is the data OK before you sort it?
You didn't answer this question.

Post your latest code.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Apr 13th, 2006
0

Re: Passing a Value between an Array and Another Function

The data is fine before the sort takes place.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Saint48198 is offline Offline
9 posts
since Apr 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: werid can someone explain this
Next Thread in C++ Forum Timeline: MFC vs WIN forms - Which road to take





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC