The task is to create a Group group containing an array of objects of the Student class, and add to it a menu for working with a group. I decided to implement a menu with mouse controls. The problem is that when I try to add new students to the group, cin on the number of students does not work - there is a cursor, but input cannot be made. And after pressing a few random keys (but not all), the program crashes with some kind of error (in different cases, the errors are different). I enclose the necessary function code (everything superfluous or incomprehensible is replaced / supplemented by comments).
group &group::menu() { CONSOLE_SCREEN_BUFFER_INFO bi; GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &bi); /*здесь вывод меню*/ cout << endl; const int menu_size = 7; hovered_area menu[menu_size];//hovered_area - структура, хранящая координаты однострочной прямоуг. области for (int i = 0; i < menu_size; i++) { menu[i].left_border = 0; menu[i].right_border = 25; menu[i].height = bi.dwCursorPosition.Y + 1 + i; } return mouse(menu); } group &group::mouse(hovered_area *area) { int menu_size = 7; DWORD dwOldMode, dwMode, dwNumRead; GetConsoleMode(hStdIn, &dwOldMode);//hStdIn объявлен в заголовочном файле dwMode = ENABLE_MOUSE_INPUT; SetConsoleMode(hStdIn, dwMode); INPUT_RECORD irInBuf[128]; COORD mousePos; DWORD mouseButtonState; while (1) { ReadConsoleInput(hStdIn, irInBuf, 128, &dwNumRead); for (DWORD i = 0; i < dwNumRead; i++) { mousePos = irInBuf[i].Event.MouseEvent.dwMousePosition; if (irInBuf[i].Event.MouseEvent.dwEventFlags == MOUSE_MOVED) { for (int i = 0; i < menu_size; i++) { /*подсвечивание выбранного пункта при наведении*/ } } mouseButtonState = irInBuf[i].Event.MouseEvent.dwButtonState; if (mouseButtonState == FROM_LEFT_1ST_BUTTON_PRESSED) { for (int j = 0; j < menu_size; j++) { if (mousePos.X >= area[j].left_border && mousePos.X <= area[j].right_border && mousePos.Y == area[j].height) { char name[100]; int homeworks, done_homeworks; int *marks; int n; vector<student> a; switch (j) { /*case 0-2*/ case 3: marks = new int[1]; marks[0] = -1; delete[] marks; cout << "Введите количество добавляемых студентов: "; cin >> n;//проблемный cin for (int k = 0; k < n; k++) { cout << "Введите имя студента: "; cin.getline(name, 100); cout << "Введите количество домашних заданий: "; cin >> homeworks; cout << "Введите количество выполненных домашних заданий: "; cin >> done_homeworks; if (done_homeworks != 0) { marks = new int [done_homeworks]; cout << "Введите оценки: "; for (int l = 0; l < done_homeworks; l++) { cin >> marks[i]; } } if (done_homeworks != 0) { a.push_back(student(name, homeworks, done_homeworks, marks)); } else { a.push_back(student(name, homeworks)); } } *this += group("", a); return *this; /*case 4-6*/ } } } } } } SetConsoleMode(hStdIn, dwOldMode); } If necessary, I can add a question.
dwMode = ENABLE_MOUSE_INPUT; SetConsoleMode(hStdIn, dwMode);dwMode = ENABLE_MOUSE_INPUT; SetConsoleMode(hStdIn, dwMode);? That is, you stupidly turn off all console modes, leaving onlyENABLE_MOUSE_INPUT. Maybe it was necessary to add theENABLE_MOUSE_INPUTmode to the current modes, and not to replace them with all other modes? - AnTdwMode = dwOldMode | ENABLE_MOUSE_INPUTcorrectdwMode = dwOldMode | ENABLE_MOUSE_INPUTdwMode = dwOldMode | ENABLE_MOUSE_INPUT. But I may be wrong. - AnT