I have got this code:
int n,m; // n - rows count , m - columns in each row count
cin >> n >> m;
string wiersze[n];
int wynik=0;
for(int i = 0; i < n;i++)
{
cin >> wiersze[i];
}
for(int i = 0; i < n;i++)
{
for(int j =0; j<m-1;j++)
{
cout << j << " " << j+1<<endl; // debbuging line
cout<<wiersze[i].at(j)<<wiersze[i].at(j+1)<<endl; // debbuging line
if(wiersze[i].at(j) == wiersze[i].at(j+1))wynik++;
}
}
When there are two the letters in the string, I should increase variable wynik
.
I do not know why, but running that code on this example:
4 4
aaba
aaab
aaba
bbaa
causes std::out_of_range
error on string::at
in row if(wiersze[i].at(j) == wiersze[i].at(j+1))wynik++;
.
I tried debbuging and added two lines in loop to see what is happeing. Results:
0 1
aa
1 2 ab
2 3
ba
0 1
aa
1 2
aa
2 3
ab
0 1
aa
1 2
ab
2 3
ba
0 1
bb
1 2
ba
2 3
aa
Please look at two last lines - it shows exactly that what by computation next line does not exists! I, of course, tried to look for "stupid" mistakes and small debugging, but without any result exepct that, you can see above. Please help me in my case.