944,092 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1627
  • C++ RSS
May 24th, 2005
0

Array data not setting

Expand Post »
Hi, im pretty new to c++ and i have created the program below, and for some reason it sets 1 or 2 arrays right then the rest just 0's, when it uses the same code, and the data is getting threw to the calls etc... i have no idea why it doesnt set the arrays.

I have lots of testing that outputs whats going on ... and you can see that it should set the data cause it sets 1 or 2 before it...

So if im doing something wrong (which i probably am, as i said im new) i would greatly appreciate it.

Get the grades.txt data below and make it a txt file then run the code.

When u run the program you can just enter:
2
grades.txt

Grades.txt data below:


000101 73
000102 56.5
000135 88.3
123456 67.3
990001 64.5
990002 46
000103 57
101011 58
010303 59
592101 60
414332 68
432131 69
145234 70
546525 71.7


C++ Syntax (Toggle Plain Text)
  1. //grades.cpp
  2. #include <iostream>
  3. #include <iomanip>
  4. #include <fstream>
  5. using namespace std;
  6.  
  7. // Instead of making variables of how many students per grade, average and stand deviation
  8. // per grade, i will just make 3 arrays and store them from 0=HD - 4=F
  9. // When i had aStuGrades as aStuGrades[4] it created an error for the data that was
  10. // in the 4th section (aStuGrades[4]) and changing it to 5 seemed to fix it.
  11. int aStuGrades[5]; // Store how many students per grade in array
  12. float aAv[5]; // Store the average of each grade in array
  13. float aStndDev[5]; // Store the standard deviation of each grade in array
  14.  
  15. string aGrades[]={"H","D","C","P","F"};
  16.  
  17. string aHStuID[299];
  18. float aHMark[299];
  19.  
  20. string aDStuID[299];
  21. float aDMark[299];
  22.  
  23. string aCStuID[299];
  24. float aCMark[299];
  25.  
  26. string aPStuID[299];
  27. float aPMark[299];
  28.  
  29. string aFStuID[299];
  30. float aFMark[299];
  31.  
  32. void CalculateGrade(string iStuID, float fStuMark){
  33. int iGrade;
  34. iGrade = 0;
  35. if (fStuMark >= 85) {
  36. aHStuID[aStuGrades[iGrade]] = iStuID;
  37. aHMark[aStuGrades[iGrade]] = fStuMark;
  38. iGrade = 0;
  39. }else if ((fStuMark >= 75) && (fStuMark < 85)) {
  40. aDStuID[aStuGrades[iGrade]] = iStuID;
  41. aDMark[aStuGrades[iGrade]] = fStuMark;
  42. iGrade = 1;
  43. }else if ((fStuMark >= 65) && (fStuMark < 75)) {
  44. aCStuID[aStuGrades[iGrade]] = iStuID;
  45. aCMark[aStuGrades[iGrade]] = fStuMark;
  46. iGrade = 2;
  47. }else if ((fStuMark >= 50) && (fStuMark < 65)) {
  48. aPStuID[aStuGrades[iGrade]] = iStuID;
  49. aPMark[aStuGrades[iGrade]] = fStuMark;
  50. iGrade = 3;
  51. }else if (fStuMark < 50) {
  52. aFStuID[aStuGrades[iGrade]] = iStuID;
  53. aFMark[aStuGrades[iGrade]] = fStuMark;
  54. iGrade = 4;
  55. }
  56. aAv[iGrade] = aAv[iGrade] + fStuMark;
  57. aStuGrades[iGrade] = aStuGrades[iGrade] + 1;
  58. cout << aGrades[iGrade] << " " << aStuGrades[iGrade] << " id " << iStuID << " m " << fStuMark << endl;
  59. }
  60.  
  61. int ReadFromFile(const char * argv){
  62. int fSuccess;
  63. int i = 0;
  64. string fFile;
  65. fFile = argv;
  66. cout << "Reading data from " << fFile << endl;
  67. ifstream inFile;
  68. inFile.open(argv);
  69. string iStuID;
  70. float fStuMark;
  71. if (!inFile) {
  72. fSuccess = 0;
  73. }else{
  74. fSuccess = 1;
  75. // Set arrays values to 0
  76. for(int x=0;x<=4;x++){
  77. aStuGrades[x] = 0;
  78. aAv[x] = 0;
  79. aStndDev[x] = 0;
  80. }
  81. while (inFile >> iStuID >> fStuMark) {
  82. CalculateGrade(iStuID, fStuMark);
  83. }
  84. inFile.close();
  85. }
  86. return fSuccess;
  87. }
  88.  
  89. int main(int argc, char *argv[]){
  90. if (argc == 2){
  91. if (ReadFromFile(argv[1]) == 0){
  92. cout << "Unable to open " << argv[1] << endl;
  93. system("pause");
  94. return 0;
  95. }
  96. }else{
  97. int iEntData;
  98. const char * c_str();
  99. string sFiletoRead;
  100. string iStuID;
  101. float fStuMark;
  102. cout << "How would you like to enter data [1/2] ?\n\t1 - Manual input\n\t2 - From data file" << endl;
  103. cin >> iEntData;
  104. switch (iEntData) {
  105. case 1:
  106. cout << "Manual Entry:\nPlease enter Student ID then mark as shown below\n-> 123456 99.9\nCTRL + Z to finish." << endl;
  107. // Set arrays values to 0
  108. for(int x=0;x<=4;x++){
  109. aStuGrades[x] = 0;
  110. aAv[x] = 0;
  111. aStndDev[x] = 0;
  112. }
  113. while (!cin.eof()){
  114. cin >> iStuID >> fStuMark;
  115. if (!cin.eof()){
  116. CalculateGrade(iStuID, fStuMark);
  117. }
  118. }
  119. break;
  120. case 2:
  121. cout << "Please enter the file you would like to read" << endl;
  122. cin >> sFiletoRead;
  123. if (ReadFromFile(sFiletoRead.c_str()) == 0){
  124. cout << "Unable to open " << sFiletoRead << endl;
  125. system("pause");
  126. return 0;
  127. }
  128. break;
  129. default:
  130. cout << "Invalid integer entered\n";
  131. system("pause");
  132. return 0;
  133. break;
  134. }
  135. }
  136. double tmp;
  137. for(int x=0;x<=4;x++){
  138. if (aStuGrades[x] != 0){
  139. aAv[x] = aAv[x] / aStuGrades[x];
  140. for(int i=0;i<aStuGrades[x];i++){
  141. switch (x) {
  142. case 0: // HD
  143. aStndDev[x] = aStndDev[x] + pow((aHMark[i] - aAv[x]),2);
  144. cout << aGrades[x] << " sd " << aStndDev[x] << " i " << i << " id " << aHStuID[i] << " m " << aHMark[i] << " av " << aAv[x] << endl;
  145. break;
  146. case 1: // D
  147. aStndDev[x] = aStndDev[x] + pow((aDMark[i] - aAv[x]),2);
  148. cout << aGrades[x] << " sd " << aStndDev[x] << " i " << i << " id " << aDStuID[i] << " m " << aDMark[i] << " av " << aAv[x] << endl;
  149. break;
  150. case 2: // C
  151. aStndDev[x] = aStndDev[x] + pow((aCMark[i] - aAv[x]),2);
  152. cout << aGrades[x] << " sd " << aStndDev[x] << " i " << i << " id " << aCStuID[i] << " m " << aCMark[i] << " av " << aAv[x] << endl;
  153. break;
  154. case 3: // P
  155. aStndDev[x] = aStndDev[x] + pow((aPMark[i] - aAv[x]),2);
  156. cout << aGrades[x] << " sd " << aStndDev[x] << " i " << i << " id " << aPStuID[i] << " m " << aPMark[i] << " av " << aAv[x] << endl;
  157. break;
  158. case 4: // F
  159. aStndDev[x] = aStndDev[x] + pow((aFMark[i] - aAv[x]),2);
  160. cout << aGrades[x] << " sd " << aStndDev[x] << " i " << i << " id " << aFStuID[i] << " m " << aFMark[i] << " av " << aAv[x] << endl;
  161. break;
  162. }
  163. }
  164. tmp = aStndDev[x] / aStuGrades[x];
  165. //aStndDev[x] = pow(tmp,0.5);
  166. cout << aStuGrades[x] << " students " << aGrades[x] << " av: " << aAv[x] << " sd: " << aStndDev[x] << " . " << tmp << endl;
  167. }
  168. }
  169. system("pause");
  170. return 0;
  171. }


This is the output once u run the program:

C++ Syntax (Toggle Plain Text)
  1. Below is setting the arrays and you can see the data is getting through (student id and student mark), and it is calculating them because of the grade (H, D, C, P, F)... so i dont know whats wrong
  2. C 1 id 000101 m 73
  3. P 1 id 000102 m 56.5
  4. H 1 id 000135 m 88.3
  5. C 2 id 123456 m 67.3
  6. P 2 id 990001 m 64.5
  7. F 1 id 990002 m 46
  8. P 3 id 000103 m 57
  9. P 4 id 101011 m 58
  10. P 5 id 010303 m 59
  11. P 6 id 592101 m 60
  12. C 3 id 414332 m 68
  13. C 4 id 432131 m 69
  14. C 5 id 145234 m 70
  15. C 6 id 546525 m 71.7
  16.  
  17. This is the output of the actual arrays
  18. and u can see for each array 1 or 2 work and the rest the data isnt set.
  19. H sd 0 i 0 id 000135 m 88.3 av 88.3
  20. 1 students H av: 88.3 sd: 0 . 0
  21. C sd 10.0278 i 0 id 000101 m 73 av 69.8333
  22. C sd 13.5122 i 1 id 546525 m 71.7 av 69.8333
  23. C sd 4890.21 i 2 id m 0 av 69.8333
  24. C sd 9766.9 i 3 id m 0 av 69.8333
  25. C sd 14643.6 i 4 id m 0 av 69.8333
  26. C sd 19520.3 i 5 id m 0 av 69.8333
  27. 6 students C av: 69.8333 sd: 19520.3 . 3253.38
  28. P sd 7.11112 i 0 id 000102 m 56.5 av 59.1667
  29. P sd 7.80556 i 1 id 592101 m 60 av 59.1667
  30. P sd 3508.5 i 2 id m 0 av 59.1667
  31. P sd 7009.19 i 3 id m 0 av 59.1667
  32. P sd 10509.9 i 4 id m 0 av 59.1667
  33. P sd 14010.6 i 5 id m 0 av 59.1667
  34. 6 students P av: 59.1667 sd: 14010.6 . 2335.1
  35. F sd 2116 i 0 id m 0 av 46
  36. 1 students F av: 46 sd: 2116 . 2116
Similar Threads
Kus
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Kus is offline Offline
2 posts
since May 2005
May 25th, 2005
0

Re: Array data not setting

its alright i fixed it ...
Kus
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Kus is offline Offline
2 posts
since May 2005

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: How to get Directory and File names
Next Thread in C++ Forum Timeline: Visual Studio 6: Initializing static class member





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


Follow us on Twitter


© 2011 DaniWeb® LLC