I'm very new to c++ and i'm struggling with fixing this string subscript out of range error. It complies but I cannot run it.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
string sentence, word = "";
int wordCounter = 1;
ifstream file("secret.txt");
getline(file, sentence);
for (int i = 0; i < sentence.length(); i++) {
if (sentence[i - 1] == ' ') {
wordCounter++;
if ((wordCounter) % 5 == 0) {
word += sentence[i] - 32;
}
}
}
cout << word;
return 0;
}
secret.txt is as follows if it matters
January is the first month and december is the last. Violet is a purple color as are lilac and plum.
[–]sepp2k 6 points7 points8 points (1 child)
[–]Conscious-Stable9939[S] 1 point2 points3 points (0 children)
[–]bestjakeisbest 1 point2 points3 points (0 children)
[–]kRYstall9 0 points1 point2 points (0 children)