D
D
dzzaad2022-04-02 17:54:55
C++ / C#
dzzaad, 2022-04-02 17:54:55

Why does he print the evaluation of the last person again and on a new line in while?

The code
#include <iostream>
#include <Windows.h>
#include <fstream>
using namespace std;
struct Student
{
    char firstname[20], name[20], patronymic[20];
    int math, physics, drawing, chemistry, copromat;
};
int main() {
    SetConsoleCP(1251);
    SetConsoleOutputCP(1251);
    ofstream zapis;
    zapis.open("first_file.txt", ios_base::out | ios_base::trunc | ios_base::binary);
    struct Student cheliki;
    int count_students, i = 1;
    float average_mark, count_physics_mark = 0.0;
    cout << "Введите количество учеников: ";
    cin >> count_students;
    while (i <= count_students) {
        cout << "Введите фамилию " << i << "-ого ученика: ";
        cin >> cheliki.firstname;
        cout << "Введите имя " << i << "-ого ученика: ";
        cin >> cheliki.name;
        cout << "Введите отчество " << i << "-ого ученика: ";
        cin >> cheliki.patronymic;
        cout << "Введите оценку " << i << "-ого ученика по математике: ";
        cin >> cheliki.math;
        cout << "Введите оценку " << i << "-ого ученика по физике: ";
        cin >> cheliki.physics;
        count_physics_mark += cheliki.physics;
        cout << "Введите оценку " << i << "-ого ученика по черчению: ";
        cin >> cheliki.drawing;
        cout << "Введите оценку " << i << "-ого ученика по химии: ";
        cin >> cheliki.chemistry;
        cout << "Введите оценку " << i << "-ого ученика по сопромату: ";
        cin >> cheliki.copromat;
        i++;
        zapis << cheliki.firstname << " " << cheliki.name << " " << cheliki.patronymic << " " <<  cheliki.physics << endl;
    }
    ifstream rFile;
    rFile.open("first_file.txt", ios_base::in | ios_base::binary);
    while (!rFile.eof()) {
        rFile >> cheliki.firstname >> cheliki.name >> cheliki.patronymic >> cheliki.physics;
        cout << cheliki.firstname << cheliki.physics << ' ' << endl;
    }
    average_mark = count_physics_mark / count_students;
    cout << "Средний балл группы по физике = " << average_mark;
    rFile.close();
    return 0;
}

6248639e976a3039622039.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Alexandrov, 2022-04-02
@dzzaad

zapis << cheliki.firstname << " " << cheliki.name << " " << cheliki.patronymic << " " << cheliki.physics << endl;
with such a record (endl at the end), the file will always be with an empty line at the end. Accordingly, when outputting from a file, the cycle is made 1 time larger than necessary.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question