C++ Code to Export Students Details to Text Document

Read students details, calculate the grades of students and export those results to the external text document.

#include <iostream>
#include <string>
#include <fstream>
#include <conio.h>
using namespace std;
 
class student
{
 
public:
    student()
    {
    Name = "";
    ID = 0;
    Mid1 = 0;
    Mid2 = 0;
    Final = 0;
    HWs = 0;
    Total = 0;
    Grade = 'F';
 
    fout.open("StudentsInfo.txt", ios::app);
    }
 
    string getName()
    {
    return Name;
    }
 
    bool setStud(string name, long int id, int m1, int m2, int f, int h)
    {
    Name = name;
    ID = id;
 
    if((m1 >= 0) && (m2 <= 100))
        Mid1 = m1;
    else
        return false;
 
    if((m2 >= 0) && (m2 <= 100))
        Mid2 = m2;
    else
        return false;
 
    if((f >= 0) && (f <= 100))
        Final = f;
    else
        return false;
 
    if((h >= 0) && (h <= 100))
        HWs = h;
    else
        return false;
 
    Total = (0.25 * m1) + (0.25 * m2) + (0.40 * f) + (0.10 * h);
 
    if(Total >= 90)
        Grade = 'A';
    else if(Total >= 80)
        Grade = 'B';
    else if(Total >= 70)
        Grade = 'C';
    else if(Total >= 60)
        Grade = 'D';
 
    return true;
    }
 
    void PrintStud()
    {
 
    cout << "Student name: " << Name << "\tID: " << ID << endl << "Mid 1: " << Mid1 << "\tMid 2: " << Mid2 << endl
         << "HWs  : " << HWs << "\tfinal: " << Final << endl << "Total: " << Total << "\tGrade: " << Grade
         << "\n\n";
 
    fout << "Student name: " << Name << "\tID: " << ID << endl << "Mid 1: " << Mid1 << "\tMid 2: " << Mid2 << endl
         << "HWs  : " << HWs << "\tfinal: " << Final << endl << "Total: " << Total << "\tGrade: " << Grade
         << "\n\n";
    }
 
private:
    string Name;
    long int ID;
    int Mid1, Mid2, Final, HWs;
    float Total;
    char Grade;
    ofstream fout;
};
 
int main()
{
 
    bool check;
 
    student s[15];
 
    check = s[0].setStud("Anath", 0001, 100, 90, 95, 80);
 
    if(check)
    s[0].PrintStud();
    else
    cout << "Error!\n\tThe information provided for student: " << s[0].getName() << " was incorrect\n\n";
 
    check = s[1].setStud("Balu", 0002, 99, 100, 0, 0);
 
    if(check)
    s[1].PrintStud();
    else
    cout << "Error!\n\tThe information provided for student: " << s[1].getName() << " was incorrect\n\n";
    getch();
}

 OUTPUT:

Student name: Anath     ID: 1
Mid 1: 100      Mid 2: 90
HWs  : 80       final: 95
Total: 93.5     Grade: A

Student name: Balu      ID: 2
Mid 1: 99       Mid 2: 100
HWs  : 0        final: 0
Total: 49.75    Grade: F

Leave a Reply

Your email address will not be published. Required fields are marked *