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.

  • one
    And what is this: dwMode = ENABLE_MOUSE_INPUT; SetConsoleMode(hStdIn, dwMode); dwMode = ENABLE_MOUSE_INPUT; SetConsoleMode(hStdIn, dwMode); ? That is, you stupidly turn off all console modes, leaving only ENABLE_MOUSE_INPUT . Maybe it was necessary to add the ENABLE_MOUSE_INPUT mode to the current modes, and not to replace them with all other modes? - AnT
  • @AnT in this case, if not difficult, tell me what to replace these lines with. The code for connecting the mouse was taken by me from the Internet and before it worked in my programs without creating any problems. - Dheinamar
  • I would suggest that it would be dwMode = dwOldMode | ENABLE_MOUSE_INPUT correct dwMode = dwOldMode | ENABLE_MOUSE_INPUT dwMode = dwOldMode | ENABLE_MOUSE_INPUT . But I may be wrong. - AnT
  • @AnT looks like your solution helped get rid of the problem. Thank you. - Dheinamar

0