Reputation: 11
So currently working on some exercises on I/O, my code basically takes a string from input and removes the vowels at input:
ExerciseClass.cpp:
NoVowelString& NoVowelString::operator>>(std::string& s) {
while (!(Buffer >> s)) {
if (Buffer.bad() || !(InputSource.good())) return *this;
Buffer.clear();
std::string vowelstring;
InputSource >> vowelstring;
for (int i = 0; i < vowelstring.size(); i++) {
if (!CaseSensitive) vowelstring[i] = tolower(vowelstring[i]);
if (IsVowel(tolower(vowelstring[i]))) {
vowelstring.erase(vowelstring.begin() + i);
i = -1;
}
}
Buffer.str(vowelstring);
}
return *this;
}
NoVowelString::operator bool() {
return !(InputSource.fail() || InputSource.bad()) && InputSource.good();
}
Main.cpp:
while (NoVowelStrings >> s) {
ProcessedInput.push_back(s);
}
now the thing is that it works perfectly when dealing with input from keyboard(cin istream) but for files it skips the last word, that's because Inputsource.eof()
sets off after reading the last word, without having to read after it, so in main s gets the correct value but it doesn't get into the vector, tried searching a bit online but to no avail. My guess is that something in the function that converts the stream to bool is wrong but I can't figure out what.
Debugged the code, after putting an if(InputSource.eof()) std::cout << "bad"
after the InputSource >> vowelstring
I found that it always went eof right after getting the last string, and, from what I know, the input stream should output eof trying to read after the last string. Even though i dont know for certain what cause it to fail i suppose it's the eof flag making the boolean return false.
Upvotes: 1
Views: 44