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

7
  • 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 Commented Dec 11, 2018 at 19:36
  • Are you not familiar with while loops yet? Commented Dec 11, 2018 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. Commented Dec 11, 2018 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. Commented Dec 11, 2018 at 19:39
  • @Lilo Take a look at this Commented Dec 11, 2018 at 19:48

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.