473,322 Members | 1,699 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Help with void function output

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
2 3922
boxfish
469 Expert 256MB
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
1,764 Expert 1GB
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

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

Similar topics

20
by: da Vinci | last post by:
Hello again. I have a question regaring pass-by-reference and multiple functions. This is an assignment that I have to use pass-by-reference for everything. First off, I made the following...
11
by: DrNoose | last post by:
Hi! I've got a program that's almost done, but I'm getting compile errors in two lines: 317 & 319. I think the main error has to do with the Truck Class. I'm a newbie and keep looking at the...
3
by: trialproduct2004 | last post by:
Hi all, Can someone tell me how virtual functions works. Like how memory is getting allocated to virtual function. And how to call base class function through derived class pointer. And why...
13
by: Fao | last post by:
Hello, I am having some problems with inheritance. The compiler does not not return any error messages, but when I execute the program, it only allows me to enter the number, but nothing else...
1
by: dasilva109 | last post by:
Hi guys I am new to C++ and need urgent help with this part of my code for a uni coursework I have to submit by Thursday //ClientData.h #ifndef CLIENTDATA_H #define CLIENTDATA_H #include...
2
by: dasilva109 | last post by:
Hi guys I am new to C++ and need urgent help with this part of my code for a uni coursework I have to submit by Thursday //ClientData.h #ifndef CLIENTDATA_H #define CLIENTDATA_H #include...
1
by: Tigerlily | last post by:
// Program to create new accounts, perform deposits, withdrawals, and bank //inquiries #include<iostream> #include<fstream> using namespace std; void menu(); int read_accts(int , double ,...
1
by: al2004 | last post by:
Write a program that reads information about youth soccer teams from a file, calculates the average score for each team and prints the averages in a neatly formatted table along with the team name....
22
by: Amali | last post by:
I'm newdie in c programming. this is my first project in programming. I have to write a program for a airline reservation. this is what i have done yet. but when it runs it shows the number of...
3
by: Stephen Torri | last post by:
Below is a class that is suppose to represent a segment of memory or a contents of a binary image (e.g. ELF executable). I have started to read Modern C++ Design and thought the best way to ensure...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.