Task 2
Write a program which takes 3 students’ data from file (“input.txt”) and displays the grade
obtained by student in new file (“output.txt”).
Hint: The program will read name from the file and then the marks obtained in three subjects.
Add those marks and convert them out of 100 and display the grade obtained by student.
Repeat these steps for three students
Score (out of
100)
Grade
0 – 49.5
F
49.5 – 57.5
D
57.5 – 71.5
C
71.5 – 84.5
B
>= 84.5
A
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
struct Student
{
string name;
float grade;
};
int main()
{
const int N = 3;
Student arr[N];
cout << "Please, enter an info about 3 students:\n";
for (int i = 0; i < N; i++)
{
cout << "Please, enter a name of " << i + 1 << " student: ";
cin >> arr[i].name;
cout << "Please, enter a grade of " << i + 1 << " student: ";
cin >> arr[i].grade;
}
ofstream of;
of.open("Input.txt");
if (!of.is_open())
cout << "File isn`t opened!";
else
{
for (int i = 0; i < N; i++)
{
of << arr[i].name << " " << arr[i].grade << endl;
}
}
of.close();
ifstream ifs("input.txt");
ofstream ofs("output.txt");
float grades[N];
if (!ifs.is_open()||!ofs.is_open())
cout << "File isn`t opened!";
else
{
string line;
int i = 0;
while (getline(ifs, line, '\n'))
{
string str = line.substr(0, line.find(' '));
str = line.substr(line.find(' ') + 1);
ofs << str<<endl;
grades[i++] = stof(str);
}
}
for (int i = 0; i < N; i++)
{
cout << "\nGrade of student "<<i+1<<" is ";
if (grades[i] > 0 && grades[i] < 49.5)
cout << "F";
else if(grades[i] >= 49.5 && grades[i] < 57.5)
cout << "D";
else if (grades[i] >= 57.5 && grades[i] < 71.5)
cout << "C";
else if (grades[i] >= 71.5 && grades[i] < 84.5)
cout << "B";
else if (grades[i] >=84.5)
cout << "A";
}
}
Comments
Leave a comment