My input file is:
Title: Titanic
17:40 hall 1
19:20 hall 7
20:20 hall 1
Title: To Kill a Mockingbird
14:10 hall 3
15:50 hall 3
18:25 hall 8
20:30 hall 2
Title: The Silence of the Lambs
19:30 hall 5
20:50 hall 4
22:10 hall 3
My code:
const std::string filename = "schedule.txt";
std::string movieName, movieTime, movieHall;
std::ifstream file (filename);
if (file.is_open()) {
getline(file, movieName);
file >> movieTime >> movieHall;
file.close();
}
else
std::cout << "unable to open file";
I don't know how to create a loop which would save every movieTime and movieHall for each movie and then go on to another movie and its movieTime/movieHall. I tried with find, but the programme finds the first "title" and then just saves everything randomly to time and hall, it doesn't stop on another title to read it with getline.
EDIT
Solved my problem with std::istringstream
const std::string filename = "schedule.txt";
std::string movieName, movieTime, movieHall, read;
std::ifstream file (filename);
if (file.is_open()) {
while(getline(file, read)){
std::istringstream iss(read);
std::string phrase;
if( std::getline(iss, phrase, ' ') ){
if(phrase == "Title")
{
std::cout << read << std::endl;
}
else
{
file >> movieTime >> movieHall;
std::cout << movieTime << " " << movieHall << std::endl;
}
}
}
file.close();
}
else
std::cout << "unable to open file";
Thank you (again) @Fubert