txt.file

Write your question here.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
 #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;
} 
for readFile read in all student ID's into the integer array you passed in. Read all the other grades in the double array.

When you calculate the percentages, just use the value in the double array in sets of three, since there are three for every student.

You could do the percentage function inside the writeFile() or outside and pass in a double array with the percentages already in. In the for-loop, just use the values doubleArray[i], doubleArray[i+1], and doubleArray[i+2]. Then instead of i++, use i += 3. There are prettier ways of storing these values (a class or even a struct would be great) but this would work.

You probably don't have to clear myfile, but you might as well anyway myfile.clear(). You may also want to call it myFile, since the prompt stresses good programming skills, which I can only assume implies good conventions like lowerCamelCase for variables.

Also, and I don't know why, system("PAUSE") is considered incredibly bad practice. I'm just a beginner but the better programmers on this forum have a tendency to freak out when they see it used improperly. I like to put a cin.ignore() as it does virtually the same thing without messing with system("ANYTHING")
Last edited on
thnks!
Topic archived. No new replies allowed.