There's a behavior that I don't understand, the c
variable is supposed to increase by one every time an element of the array isn't equal to the one next to it, but there's one increment that's done automatically which increases c
by one. I think this is because the last element of the array is compared to something which isn't equal to it. Why is this happening? Or if I'm right what's the thing that the last element is compared to?
#include <iostream>
int main() {
int n, c = 0;
std::cin >> n;
std::string s[n];
for (int i = 0; i < n; i++) {
std::cin >> s[i];
}
for (int i = 0; i < n; i++) {
if (s[i] != s[i+1]) {
c++;
}
}
std::cout << c;
}