This is my code trying to solve a problem with C++ that determines the max length of a substring of a character in a row. example input : ATTCGGGA
| output : 3
The error I got when run in the terminal is :
what(): basic_string::at: __n (which is 9) >= this->size() (which is 9)
#include <bits/stdc++.h>
#include <iostream>
#include <limits.h>
using namespace std;
int main(){
string n;
cin >> n;
int length = sizeof(n);
int tempCount = 1;
int answer;
int x = 0;
while (x < length) {
if (n.at(x) == n.at(x+1)) {
tempCount += 1;
} else tempCount = 1;
if (tempCount > answer) {
answer = tempCount;
}
x++;
}
cout << answer << endl;
}