In: Computer Science
C++
Write a program that asks a teacher to input a student’s first name, last name, and four test scores. The program should find the average of the four test scores and should then write the following information to a file named “students.txt”
last_name
first_name
average
A student's first name of “XX” should be used as a sentinel value and no numeric grades less than 0 or greater than 100 should be accepted. The program should then read the information in for the file “students.txt”, and for each student display the following on the monitor:
first_name last_name average
first_name last_name average
…
Number of students n
Overall average: nn.n
Note: all averages should be displayed with one decimal position
Below is the source code. Please comment if any queries or issues. Thanks.
#include <iostream>
#include<bits/stdc++.h>
#include <fstream>
using namespace std;
class Student{
public:
string firstName;
string lastName;
float marks[4];
float average;
};
int main()
{
char cont = 'N';
float exam1[100], total = 0 , total_average = 0;
int count=0;
ifstream infile;
string line;
do {
Student s;
cout<<"Enter Student's first name"<<endl;
cin>>s.firstName;
cout<<"Enter Student's Last Name" <<endl;
cin>>s.lastName;
cout<<"Enter Marks"<<endl;
float marks;
for (int i = 0; i < 4 ; i++){
cin>>marks;
if (marks <0 || marks > 100){
cout<<"Marks cannot be less than 0 or greater than 100, please enter again"<<endl;
i = i-1;
continue;
}
else
s.marks[i] = marks;
}
float sum = 0;
for (int i = 0; i < 4; i++){
sum = sum + s.marks[i];
}
s.average = sum/4;
char filename[ ] = "students.txt";
ofstream outputfile;
// creating, opening and writing/appending data to a file
outputfile.open(filename, ios::out|ios::app);
// simple error handling for file creating/opening for writing, test if fail to open the file
if(outputfile.fail())
{
cout<<"The "<<filename<<" file could not be created/opened!\n";
exit(1); // just exit
}
else
{
cout<<"The "<<filename<<" file was created and opened successfully!\n";
// outputfile<<"Writing some data to this file\n";
//outputfile<<"------------------------------\n";
cout<<"Check the "<<filename<<" file contents :-)"<<endl;
cout<<"If the file already have had data, the new data will be appended\n";
outputfile<<s.firstName<<" "<<s.lastName<< " " << fixed << setprecision(1) << s.average;
outputfile<<endl;
outputfile.close();
if(outputfile.fail())
{
cout<<"The "<<filename<<" file could not be closed!\n";
exit(1);
}
// test if successful to close the file, do the following...
else
cout<<"\nThe "<<filename<<" file was closed successfully!\n";
}
cout<<"Enter Y/y if there is another student"<<endl;
cin>>cont;
}while(cont == 'Y' || cont == 'y');
ifstream myfile ("students.txt");
if (myfile.is_open())
{
while ( getline (myfile,line) )
{
cout << line << '\n';
}
myfile.close();
}
else cout << "Unable to open file";
//To calcualte the average
cout<< "Displaying the Student Data\n";
infile.open("students.txt");// file containing numbers in 3 columns
if(infile.fail()) // checks to see if file opended
{
cout << "error" << endl;
return 1; // no point continuing if the file didn't open...
}
while(getline(infile, line)) // reads file to end of *file*, not line
{
stringstream ss(line);
char a[50], b[50];
float c;
if (ss >> a >> b >> c)
{
exam1[count] = c;
count++;
}
}
infile.close();
cout << "Number of students " << count <<"\n";
for (int i=0; i<count;i++){
total = total + exam1[i];
}
total_average = total/count;
cout << "Overall average: " << fixed << setprecision(1) << total_average;
return 0;
}
Below is the execution screenshot