The reason you are getting the error is:
currentChar = input[y];
The problem with this line is that input[y] might be inaccessible, for instance your input is "Hello". With the way you wrote your code, y would eventually equals 5. However, you the last letter 'o' is input[4]. Because of that, when y=5, you get an error.
There are couple ways you could fix it. One has already mentioned using .size() to make sure, another one has mentioned using iterator:
for(auto iterator = input.begin();iterator!=input.end();++iterator)
However, what I want to mention is that you should be declaring your currentChar and previousChar as char type instead of string since they would always be single characters. Also, depends on the version of C++ and compiler you are using, if you are using C++11, input[5] on "Hello" is actually fine.
When you passed input[5] to a char currentChar type, currentChar gets a null character. So for you while loop, you could test while(currentChar). When currentChar gets the null character, the while loop would test false, and ends the loop.
Below is how I would change inside your for loop. Most of them stays the same, beside changing previousChar and currentChar into char type, and some condition checkings for them, with some minor changes to refine them.
#include <iostream> #include <string> int main() { int num; std::cout << "How many lines of input? \n"; std::cin >> num; for (int x = 0; x < num; x++) { std::string input; std::cin >> input; char previousChar = input[0]; int y = 1; char currentChar = input[y]; int characterCount = 1; while(currentChar) { y++; if (previousChar == currentChar) { characterCount++; } else { std::cout << characterCount << " " << previousChar << "\n"; characterCount = 1; } previousChar = currentChar; currentChar = input[y]; } std::cout << characterCount << " " << previousChar << "\n"; } }
Also do note that it feels weird to use a while loop when it is iterating through a index number for me. So I would probably use a for loop instead of the while loop:
#include <iostream> #include <string> int main() { int num; std::cout << "How many lines of input? \n"; std::cin >> num; for (int x = 0; x < num; x++) { std::string input; std::cin >> input; char previousChar = input[0]; int y = 1; char currentChar = input[y]; int characterCount = 0; for(int y = 1; currentChar; y++) { if (previousChar == currentChar) { characterCount++; } else { std::cout << characterCount << " " << previousChar << "\n"; characterCount = 1; } previousChar = currentChar; currentChar = input[y]; } std::cout << characterCount << " " << previousChar << "\n"; } }
And here is how you could do it with iterator:
#include <iostream> #include <string> int main() { int num; std::cout << "How many lines of input? \n"; std::cin >> num; for (int x = 0; x < num; x++) { std::string input; std::cin >> input; char previousChar, currentChar; previousChar = input[0]; int characterCount = 1; for(auto it = input.begin()+1; it != input.end();it++) { currentChar = *it; if (previousChar == currentChar) { characterCount++; } else { std::cout << characterCount << " " << previousChar << "\n"; characterCount = 1; } previousChar = currentChar; } std::cout << characterCount << " " << previousChar << "\n"; } }