0

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;
}
Lee Cox
  • 11
  • 2
  • 2
    `int length = sizeof(n);` -- Explain what you are trying to accomplish here. That is not the way to get the number of characters in a `std::string`. Second, you should use descriptive variable names, not single letter names like `n`. – PaulMcKenzie Oct 06 '20 at 18:40
  • @PaulMcKenzie Yeah sorry I'm very new to coding but 'n' is just a string that is input given by the problem, I'm trying to make a loop that will repeat as long as x is smaller than the length of the string n, the loop is to check each character as well as the character after it comparing them if they are the same I'm adding it to a temporary answer until the two characters don't match and then I'm seeing if the temporary answer is a higher int than answer (final answer) and assigning it to the final answer. – Lee Cox Oct 06 '20 at 18:50
  • Obligatory Do not use `#include ` ([why](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h)) and avoid `using namespace std;`([why](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice)) comment. Almost certainly not the problem you've encountered, but when you encounter the problems this combination causes, it can soak a LOT of time. – user4581301 Oct 06 '20 at 19:23

3 Answers3

2

Change length like this:

int length = n.length();

and this line is wrong, in your while loop:

if (n.at(x) == n.at(x+1)

your lenght is 5 for aaaaa and max index is 4. But in your while loop when x is 4, last loop, x+1 is 5. But you have not fifth index. you must be like this:

while(x < length - 1)

My English skills are not good. I tried to explain :(

Osman Durdag
  • 955
  • 1
  • 7
  • 18
  • Thanks for the response but it's still giving me the same error when run and given an input of aaaaa it says : what(): basic_string::at: __n (which is 5) >= this->size() (which is 5) – Lee Cox Oct 06 '20 at 19:00
  • omg I got this :) I edited my answer now please check it :) – Osman Durdag Oct 06 '20 at 19:02
1

In this loop:

while (x < length) {
    if (n.at(x) == n.at(x+1)) {

you are accessing x+1, which for the last x goes out of bounds

The good news is - in 2020 you no longer need to use the length(), at(), etc. Just use

for(auto x: n)

Here is how you can do it:

int main() {
  string n;
  cin >> n;

  int tempCount(0);
  int answer(0);
  char prev(0);
  for (auto x : n) {
    if (x == prev) {
      ++tempCount;
    }
    else {
      prev = x;
      tempCount = 1;
    }
    if (tempCount > answer) {
      answer = tempCount;
    }
  }
  cout << answer << endl;
}
Vlad Feinstein
  • 10,960
  • 1
  • 12
  • 27
0

This is my fixed code it does what it's meant to and thanks everyone for the input I tried my best to use the feedback in my code :) :

#include <bits/stdc++.h>
#include <iostream>
#include <limits.h>
using namespace std;

int main(){
    string n;
    cin >> n;

    int length = n.length();
    int tempCount = 1;
    int answer = 0;
    int x = 0;

    while (x < length - 1) {
        if (n.at(x) == n.at(x+1)) {
            tempCount += 1;
            if (tempCount > answer) {
            answer = tempCount;
            }
        } else tempCount = 1;
        x++;
    }
    if (answer > 0){
        cout << answer << endl;
    } else cout << 1 << endl;

    return 0;
}
Lee Cox
  • 11
  • 2
  • It's recommended that you avoid using `#include ` and `using namespace std;` the reasons are here: [https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) and here: [https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – drescherjm Oct 06 '20 at 20:04
  • @drescherjm Yeah I saw someone else recommended that too but to be honest I don't know what I would need to put instead of it I only started trying to code properly a few days ago and those things are how my ide (CodeBlocks) automatically sets up a project and I didn't really get what the people in those threads were saying I should do instead. If you would care to explain what you do instead that would be great but even if not thanks for the advice :) – Lee Cox Oct 06 '20 at 22:25
  • For this program you can delete `#include ` and add `#include ` your code also does not need `#include ` – drescherjm Oct 06 '20 at 22:27
  • 1
    Try an empty string as an input (you can do it by `Ctrl+Z`). – Vlad Feinstein Oct 06 '20 at 22:33
  • @VladFeinstein good point. The code does not handle this condition. – drescherjm Oct 07 '20 at 12:41