0

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

Lilo
  • 7
  • 3
  • It sounds like you need to start learning collections. Things like arrays, linked list, etc. in C++ the most common collection is an array wrapper called `std::vector` – AndyG Dec 11 '18 at 19:36
  • Are you not familiar with `while` loops yet? – R Sahu Dec 11 '18 at 19:37
  • You're going to have to do some string parsing to figure out which line is which. Read the whole line in using `getline` and then check if it starts with `Title:` If it starts `Title:` then you know you have the name of a movie. If it doesn't then you know it is the time for last movie read. – NathanOliver Dec 11 '18 at 19:38
  • I am familiar with lists, arrays and structures, but I didn't consider them necessary here. I tried using while with while(file >> movieTime >> movieHall) but didn't know how to stop the loop at movieTitle. – Lilo Dec 11 '18 at 19:39
  • @Lilo Take a look at [this](https://stackoverflow.com/a/2881987/10729041) – Fubert Dec 11 '18 at 19:48
  • @Fubert thank you! Also, turns out find didn't work for me, because it only works for the fist title and ignores the rest. – Lilo Dec 11 '18 at 19:58
  • Probable future problem: [Why does std::getline() skip input after a formatted extraction?](https://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction) – user4581301 Dec 11 '18 at 20:14

1 Answers1

0

Try this: The titles start with Title: so don't read the whole line - just read the first string and then if it is Title: read the rest of the line into the title. And if it isn't Title: then it is a time so you can save that and read the rest of the line into the hall.

#include <iostream>
#include <fstream>

int main()
{
    const std::string filename = "schedule.txt";
    std::string test, movieName, movieTime, movieHall;
    std::ifstream file (filename);
    if (file.is_open()) {
        while (file >> test) {
            // At this point test is either "Title:" or a movie time
            if (test == "Title:") {
                // test is "Title:" so we need to save the movie title
                std::getline(file, movieName);
                std::cout << "\nMovie:" << movieName;
            } else {
                // test is a movie time so save that movie time and then read the movie hall
                movieTime = test;
                std::getline(file, movieHall);
                std::cout << ", " << movieTime << movieHall;
            }
        }
        file.close();
    }
    else
        std::cout << "unable to open file";

    return 0;
}

You can try it online here: https://onlinegdb.com/By--4j6JV

Jerry Jeremiah
  • 7,408
  • 2
  • 21
  • 27