0

Имеется часть кода:

struct St
{
    string Name;
    int math;
    int phys;
    int inf;
    int chem;
    int dateOfBirth;
    int group;
    double ball;
} Students;

void showNeedableStudents() {
    ifstream file1("list.txt");
    cout << "Enter the needable group: " << endl;
    int input_group;
    cin >> input_group;
    cout << "Enter the math mark u want: " << endl;
    int input_math;
    cin >> input_math;
    cout << "Enter the phys mark you need: " << endl;
    int input_phys;
    cin >> input_phys;
    string buf;
    cout << endl;
    cout << "\t\tFound students:" << endl;
    while (getline(file1, buf)) {
        file1 >> Students.Name >> Students.math >> Students.inf >> Students.phys >> Students.chem >> Students.dateOfBirth >> Students.group >> Students.ball;
        if (Students.group == input_group && Students.math == input_math && Students.phys == input_phys) {
            cout << Students.Name << " " << Students.math << " " << Students.inf << " " << Students.phys << " " << Students.chem << " " << Students.dateOfBirth << " " << Students.group << " " << Students.ball << endl;
        }
    }

    file1.close();

}

Файл List:

Sergei 10 10 10 10 2000 714302 10
Vasya 10 2 10 2 1999 714302 10
Petya 2 3 4 5 1998 714301 5.5

Задача - считывание данных о студентах из файла и если их оценки совпадают с введенными, то вывести данные на консоль. Однако при вводе оценок 10, 10 и группы 714302 выводит на экран только данные о втором ученике, причём 2 раза. В чём проблема?

2 ответа 2

1

Замените

while (getline(file1, buf)) {
    file1 >> Students.Name >> Students.math >> Students.inf 
          >> Students.phys >> Students.chem >> Students.dateOfBirth 
          >> Students.group >> Students.ball;

вот этим кодом:

while(file1 >> Students.Name >> Students.math >> Students.inf
      >> Students.phys >> Students.chem >> Students.dateOfBirth
      >> Students.group >> Students.ball
     ) {

Вы читаете лишнего (так что теряете первого и последнего студентов совсем) - раз, а после выхода на конец файла все равно пытаетесь читать (а чтения не происходит) - вот и получаете второго такого же студента в старых неизмененных переменных.

0

В строке

    file1 >> Students.Name >> Students.math >> Students.inf >> Students.phys >> Students.chem >> Students.dateOfBirth >> Students.group >> Students.ball;

Вы читаете из файла в структуру, только вот... До этого вы получили одну строку файла в переменную buf. Вам нужно читать из данной строки, а не из файла. Сделайте вот так:

void showNeedableStudents() {
    ifstream file1("list.txt");
    cout << "Enter the needable group: " << endl;
    int input_group;
    cin >> input_group;
    cout << "Enter the math mark u want: " << endl;
    int input_math;
    cin >> input_math;
    cout << "Enter the phys mark you need: " << endl;
    int input_phys;
    cin >> input_phys;
    string buf;
    cout << endl;
    cout << "\t\tFound students:" << endl;
    while (getline(file1, buf)) {
        stringstream myStream(buf);
        myStream >> Students.Name >> Students.math >> Students.inf >> Students.phys >> Students.chem >> Students.dateOfBirth >> Students.group >> Students.ball;
        if (Students.group == input_group && Students.math == input_math && Students.phys == input_phys) {
            cout << Students.Name << " " << Students.math << " " << Students.inf << " " << Students.phys << " " << Students.chem << " " << Students.dateOfBirth << " " << Students.group << " " << Students.ball << endl;
        }
    }

    file1.close();
}

Я создаю поток myStream из строки, в которую вы получили запись об одном ученике, и из нее уже вычитываю необходимые данные.

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.