Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <cstdlib> // урегулировывает вопрос с переменой типов данных
- #include <fstream>
- using std::string;
- using std::cout;
- using std::cin;
- using std::ofstream;
- struct Student {
- string last_name;
- string street;
- int house;
- int apartment;
- };
- void write_int(Student* array, int SIZE_ARRAY, char filename[]) {
- // делает: пишет целое число в файл
- // параметры:
- // student array: массив структуры
- // int SIZE: размер массива
- // char filename[]: имя файла для записи
- // возвращает: ничего
- cout << "начинаю печать\n";
- ofstream file;
- file.open(filename);
- for (int i = 0; i < SIZE_ARRAY; i++) {
- file << array[i].last_name << " ";
- file << array[i].street << " ";
- file << array[i].house << " ";
- file << array[i].apartment << "\n";
- }
- file.close();
- }
- int menu() {
- // делает: выводит меню
- // параметры:
- // возвращает: целое число - выбор пользователя
- int user_choice;
- cout << "\n[0] - check the last_name;\n";
- cout << "[1] - check the street;\n";
- cout << "[2] - check the house;\n";
- cout << "[3] - check the apartment.\n";
- cin >> user_choice;
- return user_choice;
- }
- int main() {
- int SIZE_ARRAY;
- cout << "Size: ";
- cin >> SIZE_ARRAY;
- // Student students[SIZE_ARRAY];
- Student *students;
- students = new Student[SIZE_ARRAY];
- for (int i = 0; i < SIZE_ARRAY; i++) {
- cout << "\nStudents #" << i + 1 << "\n";
- cout << "Input last_name of student: ";
- cin >> students[i].last_name;
- cout << "Input street of " << students[i].last_name << ": ";
- cin >> students[i].street;
- cout << "Input number house of " << students[i].last_name << ": ";
- cin >> students[i].house;
- cout << "Input number of apartment of " << students[i].last_name << ": ";
- cin >> students[i].apartment;
- }
- int user_choice = menu();
- char value_for_search[100];
- switch (user_choice) {
- case 0:
- cout << "last_name for search: ";
- cin >> value_for_search;
- break;
- case 1:
- cout << "street for search: ";
- cin >> value_for_search;
- break;
- case 2:
- cout << "house for search: ";
- cin >> value_for_search;
- break;
- case 3:
- cout << "apartment for search: ";
- cin >> value_for_search;
- break;
- default:
- break;
- }
- int count_param_in_array = 0;
- for (int i = 0; i < SIZE_ARRAY; i++) {
- if (user_choice == 0) {
- if (students[i].last_name == value_for_search) {
- count_param_in_array++;
- }
- }
- else if (user_choice == 1) {
- if (students[i].street == value_for_search) {
- count_param_in_array++;
- }
- }
- else if (user_choice == 2) {
- if (students[i].house == atoi(value_for_search)) {
- count_param_in_array++;
- }
- }
- else if (user_choice == 3) {
- if (students[i].apartment == atoi(value_for_search)) {
- count_param_in_array++;
- }
- }
- }
- cout << count_param_in_array << " answers found for this request\n";
- write_int(students, SIZE_ARRAY, "file.csv");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement