By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,480 Members | 1,711 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,480 IT Pros & Developers. It's quick & easy.

Help with void function output

P: 1
I am inputing from a file and I am outputting to a file. well when i run the program it just puts the headers up there and no names or grades. This is the input.
Expand|Select|Wrap|Line Numbers
  1. Balto                85    83    77    91    76
  2. Mickey          80    90    95    93    48
  3. Minnie        78    81    11    90    73
  4. Doc                92    83    30    69    87
  5. Goofy        23    45    96    38    59
  6. Duckey        60    85    45    39    67
  7. Grumpy        27    31    52    74    83
  8. Sunny        93    94    89    77    97
  9. Piggy        79    85    28    93    82
  10. Pluto                85    72    49    75    63
However, this is the output im getting:

Expand|Select|Wrap|Line Numbers
  1. Student    Test1     Test2   Test3   Test4   Test5   Average Grade 
And thats it.

Here is my code:

Expand|Select|Wrap|Line Numbers
  1.  #include <iostream>
  2.  #include <stdlib.h>
  3.  #include <iomanip>
  4.  #include <string>
  5.  #include <fstream>
  6.  
  7. using namespace std;
  8.  
  9.  
  10.  
  11. void calculateAverage(double test1, double test2, double test3, double
  12. test4, double test5, double& studentAverage);
  13. int calculateGrade(double grade);
  14.  
  15. int main()
  16. {
  17.  
  18. string studentName;
  19. int numberOfStudents = 0;
  20. double classAverage = 0;
  21. double studentAverage = 0;
  22. double totalAverage = 0; //To add the average of all student averages
  23. char grade;
  24. double test1, test2, test3, test4, test5;
  25.  
  26. ifstream inFile; // input stream variable for the student file
  27. ofstream outFile; // output stream variable
  28.  
  29. inFile.open("inGrade.txt");
  30. outFile.open("FinalGrades.txt");
  31.  
  32. cout << setfill(' ') <<setiosflags(ios:: left) << setw(11) <<
  33. "Student" << setw(10) << "Test1"
  34. << setw(8) << "Test2" << setw(8) << "Test3" << setw(8) << "Test4"
  35. << setw(8) << "Test5"
  36. << setw(8) << "Average" << setw(8) << "Grade" <<endl;
  37.  
  38. outFile << setfill(' ') <<setiosflags(ios:: left) << setw(11) <<
  39. "Student" << setw(10) << "Test1"
  40. << setw(8) << "Test2" << setw(8) << "Test3" << setw(8) << "Test4"
  41. << setw(8) << "Test5"
  42. << setw(8) << "Average" << setw(8) << "Grade" << endl;
  43.  
  44.  
  45. if (!inFile)
  46. {
  47. cout << "Unable to open the file." <<endl;
  48. return 1;
  49. }
  50.  
  51. while(inFile)
  52. {
  53. outFile.setf(ios::fixed, ios::floatfield);
  54. outFile.setf(ios::showpoint);
  55. outFile << setprecision(2);
  56.  
  57. inFile >> studentName >> test1 >> test2 >> test3 >> test4 >> test5
  58. ;
  59.  
  60. calculateAverage(test1, test2, test3, test4, test5,
  61. studentAverage);
  62.  
  63. grade = calculateGrade(studentAverage);
  64.  
  65. cout << setfill(' ') << setiosflags(ios::left) << setw(11) <<
  66. studentName << setw (10) << test1
  67. << setw(8) << test2 << setw(8) << test3 << setw(8) << test4
  68. << setw(8) << test5 << setw(9) << studentAverage << setw(8) <<
  69. grade<<endl;
  70. outFile << setfill(' ') << setiosflags(ios::left) << setw(11) <<
  71. studentName << setw (10) << test1
  72. << setw(8) << test2 << setw(8) << test3 << setw(8) << test4
  73. << setw(8) << test5 << setw(9) << studentAverage << setw(8) <<
  74. grade<<endl;
  75.  
  76. totalAverage = totalAverage + studentAverage;
  77.  
  78. numberOfStudents++;
  79. classAverage = totalAverage / numberOfStudents;
  80. }
  81.  
  82.  
  83. outFile << endl << setprecision(2)<< "Class average is:" <<
  84. classAverage << endl;
  85.  
  86. inFile.close();
  87. outFile.close();
  88.  
  89. system("PAUSE");
  90. return 0;
  91. }
  92.  
  93.  
  94.  
  95. //function to calculate the average
  96. void calculateAverage(double test1, double test2, double test3, double
  97. test4, double test5, double& studentAverage)
  98. {
  99.  
  100. studentAverage = static_cast<double>(test1 + test2 + test3 + test4
  101. + test5) / 5.0;
  102.  
  103. }
  104.  
  105.  
  106.  
  107.  
  108. int calculateGrade(double studentAverage)
  109. {
  110. char grade;
  111.  
  112. if (studentAverage <= 100 && studentAverage >= 90)
  113. grade = 'A';
  114. else if (studentAverage < 90 && studentAverage >= 80)
  115. grade = 'B';
  116. else if (studentAverage < 80 && studentAverage >= 70)
  117. grade = 'C';
  118. else if (studentAverage < 70 && studentAverage >= 60)
  119. grade = 'D';
  120. else if (studentAverage < 60 && studentAverage >= 0)
  121. grade = 'F';
  122. else
  123. cout << "Invalid grade " << endl;
  124.  
  125. return grade;
  126.  
  127. }

Any help would be greatly appreciated. Thanks.
Nov 11 '08 #1
Share this Question
Share on Google+
2 Replies


boxfish
Expert 100+
P: 469
I'm not sure. The code works fine for me and writes all the data to the file. Maybe the program is not able to open the inGrade.txt file. How about putting a system("PAUSE") before the return so you can check:
Expand|Select|Wrap|Line Numbers
  1. if (!inFile)
  2. {
  3.     cout << "Unable to open the file." <<endl;
  4.     system("PAUSE");
  5.     return 1;
  6. }
I hope this helps solve the problem.
Nov 11 '08 #2

Savage
Expert 100+
P: 1,764
Expand|Select|Wrap|Line Numbers
  1. if(!inFile)
That's not the way to check if the file is open.This call is same as calling ifstream::fail() which checks if there was a fail bit(generally some file access error) or bad bit(checks to see if stream has lost it's integrity).To check if the file is open use:

Expand|Select|Wrap|Line Numbers
  1. ifstream::is_open()
  2.  
  3. if(inFile.is_open==false)
  4. {
  5. ....
  6. }
Nov 12 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.