txt file input/output

I have been asked to write a program which will calculate the percentage and print the student ID, percentage and letter grades for the class to an output file called grades.txt. The student’s details and course marks are stored in a data file called results.txt. The class strength is 10.

(I have done first part, and iam stuck on second parts where its asks to calculate PERCENTAGE. The results.txt you need to run my code to see data)


Write a function that opens the results.txt for reading. It should test whether the file was opened correctly and if so, read in the 10 lines of data one by one into appropriate variables. The data values in the first column are stored in an integer array called StudentID, the data values from the second, third and fourth column are used to calculate the percentage which is then stored in a double precision array called Percentage.(i.e Percentage[i] = (Math + English + Physics)/3 ).
(Use the Function prototype: void readFile(int[], double[])).
 Once all data is read and stored in the appropriate arrays, close the connection to the input file.
 Write another function which will open the output file grades.txt for writing. You are to print out the student IDs, percentage and letter grades to the output file. Each student detail is to appear on a separate line of the file.
(Use the Function prototype: void writeFile(int[], double[])).
 Once all data is written, close the connection to the output file.
The letter grades for each student could be determined using the information below:
Numerical Grade Letter Grade
Greater than or equal to 90 A
Less than 90 but greater than or equal to 80 B
Less than 80 but greater than or equal to 70 C
Less than 70 but greater than or equal to 60 D
Less than 60 E

Note:
• Both the functions are to be called from the main().
• Include all appropriate header files.
• All numerical output must be formatted to display to two decimal places.
• Have appropriate commenting in your program and practice good programming skills. Your program should be well documented and user friendly.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <fstream>

using namespace std;

int main () 
{
  string line;
  
  
   ifstream infile;
   ofstream outfile;

  
  ifstream myfile ("results.txt");
  outfile.open("grades.txt", ios::out);
  if (myfile.is_open())
  {
    while ( myfile.good() )
    {
      getline (myfile,line);
      
      cout << line << endl;
      outfile<<line<<endl;
      }//End while
           
    
    myfile.close();
  } // end if

  else cout << "Unable to open file"; 



  system ("PAUSE");
  return 0;
}
 
Topic archived. No new replies allowed.