struct classes
{
string Day;
string className;
vector<string> vecClassName;
}
classes myClasses;
ifstream inputFile;
inputFile.open("schedule.txt");
while (!eof())
{
getline(inputFile, myClasses.Day, ',');
do{
getline(inputFile, myClasses.className, ',');
myClasses.vecClassName.push_back(myClasses.className);
}while (myClasses.className != "\n"); // Get all classes until end of line
// other code here --
// Compiler doesn't reach this point - the do-while loop Loops forever nonstop.
}
I don't understand why the do-while Loops forever I want it to stop until there are no classes in that line
Please Help me fix this!
I'm no expert so I'm just guessing here, but perhaps it has to do with your struct? The same classes object that holds the string for one particular class also holds the vector of all the class names, so maybe your code is getting caught in the push_back() line. Have you tried commenting anything out to see what causes the endless loop?
The do-while loop loops forever because you don't check if the end of the file is reached in the do-while loop so you should check the state of the stream.
simply add a && inputFile to the while statement to accomplish that: while (myClasses.className != "\n" && inputFile);
getline emmits the delimiter (default value for the delimiter is '\n')
so when calling getline with ',' getline reads the parts between two ','
In your example the last element of the first line and the first element of the second line are seperated by a newline but not by a comma so they won't be splitted.
The string you want to split looks like this:
{last_old_line_element + '\n' + first_new_line_element}
I think I just gave you a very good hint so good luck! ;)