Загрузка данных


#include <iostream>
#include <vector>
#include <string>
#include <limits>

class Student {
    std::string name_;
    int age_;
    int id_;
    int grade_;

public:
    Student() : name_("Не задано"), age_(0), id_(0), grade_(0) {
        std::cout << "\nСтудент создан.";
    }

    ~Student() {
        std::cout << "\nСтудент удален.";
    }

    void edit() {
        std::cout << "\nВведите имя студента: ";
        std::cin >> name_;

        std::cout << "Введите возраст студента: ";
        std::cin >> age_;

        std::cout << "Введите ID студента: ";
        std::cin >> id_;

        std::cout << "Введите класс обучения студента: ";
        std::cin >> grade_;
    }

    void print(int index) const {
        std::cout << "\n--- Студент #" << index << " ---"
                  << "\n  Имя:    " << name_
                  << "\n  Возраст: " << age_
                  << "\n  ID:      " << id_
                  << "\n  Класс:   " << grade_ << "\n";
    }
};

int getValidInt(const std::string& prompt) {
    int value;
    while (true) {
        std::cout << prompt;
        if (std::cin >> value) return value;
        std::cin.clear();
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
        std::cout << "Некорректный ввод. Попробуйте снова.\n";
    }
}

int main() {
    system("chcp 65001 > nul");

    int count = getValidInt("Введите кол-во студентов: ");
    if (count < 0) count = -count;
    if (count == 0) {
        std::cout << "Нет студентов для работы.\n";
        return 0;
    }

    std::vector<Student> students(count);

    int choice;
    do {
        system("cls");

        std::cout << "========== Список студентов ==========\n";
        for (int i = 0; i < count; i++)
            students[i].print(i);

        std::cout << "\n1. Редактировать студента\n2. Выход\n";
        choice = getValidInt("Выбор: ");

        if (choice == 1) {
            int index;
            do {
                index = getValidInt("Введите индекс студента (0–" + std::to_string(count - 1) + "): ");
            } while (index < 0 || index >= count);

            students[index].edit();
        }
    } while (choice != 2);

    return 0;
}