The program is compiled, but when you start and enter the first value, an error flies. What is the problem I can not understand. Code:

#include <iostream> #include <windows.h> #include <string> using namespace std; int main() { setlocale(0, ""); int n; string s; string temp; int numb; int mass[97]; for(int i = 0; i < 98; i++) { mass[i] = 0; } cin >> n; for(int k = 0; k < n; k++) { getline(cin, s); if(s.substr(s.length()-2, 1) == " ") { temp = s.substr(s.length()-1, 1); numb = atoi(temp.c_str()); mass[numb - 1] = mass[numb - 1] + 1; } else { temp = s.substr(s.length()-1, 1); numb = atoi(temp.c_str()); mass[numb - 1] = mass[numb - 1] + 1; } } for(int u = 0; u < n; u++) { if(mass[u] > 0) { cout << "School №" << u << ": " << mass[u] << endl; } } system("pause"); return 0; } 

Closed due to the fact that off-topic participants are Vladimir Martyanov , αλεχολυτ , Aleksey Shimansky , Duck Learns to Hide , insolor 7 May '17 at 12:41 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • “Questions asking for help with debugging (“ why does this code not work? ”) Should include the desired behavior, a specific problem or error, and a minimum code for playing it right in the question . Questions without an explicit description of the problem are useless for other visitors. See How to create minimal, self-sufficient and reproducible example . " - Vladimir Martyanov, αλεχολυτ, Alexey Shimansky, Duck Learns to Take Cover, insolor
If the question can be reformulated according to the rules set out in the certificate , edit it .

    1 answer 1

    let's not even go far ...

     int mass[97]; 

    An array of 97 elements.

     for(int i = 0; i < 98; i++) { mass[i] = 0; } 

    Elements 0, 1, 2, ..., 97 are recorded - a total of 98 pieces. Already UB.

    Further - cin >> n; . The buffer remains '\n' .

     getline(cin, s); 

    Read empty line.

     if(s.substr(s.length()-2, 1) == " ") 

    And there is an attempt to get a substring from somewhere before it ...

    Further did not even look.