Updating the Menu

Hello guys. I am doing program for a movie ticket reservation system. For that coding I'm doing a menu that will display the movies in the text file. It should display a movie and, for each movie there can be more than day assigned for each movie. The data about the movie is stored in a text file. I have done the coding which will show the showtime for only a day. So anyone can please help me to solve this?

Example of data in text file(Movies.txt):
Avengers|MON|0900|1000|1100
Superman|MON|0930|0800|0700
Batman|WED|0900|1000|1100
Spiderman|THU|0930|0800|0700

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
  void showmovielist()
{
    char name [25],time1[25],time2[25],time3[25], day[25];

    system("CLS");
    fstream movie;
    movie.open("Movies.txt",ios::in);
    int count = 0;
    string line;

    ifstream file("Movies.txt");
     while (!file.eof())
    {
        getline(file, line);
        count++;
    }

    count = count - 1;
    title();
    cout << "============================================================================================================================================" << endl;
    cout << "|SERIAL NUMBER|                   |            MOVIE NAME                |              |    DAY    |                  | SHOWTIME AVAILABLE | " << endl;
    cout << "============================================================================================================================================" << endl;
    int i = 0, j = 1;
    while(i++ , i<= count)
    {
        movie.getline(name,25,'|');
        movie.getline(day,25,'|');
        movie.getline(time1,25,'|');
        movie.getline(time2,25,'|');
        movie.getline(time3,25);
        string name1 = name;
        int name_length = name1.length();
        cout << "    (" << i << ") " << "                             " << name << std::setw(58 - name_length) << day << std::setw(30) << "    (" << j << ") " << time1 << "\n" <<
        std::setw(125) << "    (" << j + 1 << ") " << time2 << "\n" << std::setw(125) <<  "    (" << j + 2 << ") " << time3 << "\n";
        j = 1;
        cout << "============================================================================================================================================" << endl;
    }
}

void add_movie()
{
    system("CLS");
    char name [25], day[25];
    int time1;
    int time2;
    int time3;

    fstream movie;
    movie.open("Movies.txt",ios::app);

    title();
    cout << "Adding new movie!!";
    cout << endl << endl;
    cin.ignore();
    cout << " Enter the movie name: ";
    cin.getline(name,25);
    cout << " Enter the day of the showtime: ";
    cin.getline(day,25);
    cout << " Enter the time for First show  (HHMM): ";
    cin >> time1;

    while(!cin >> time1)
    {
        cout << endl;
        cout << " Enter a valid time: " ;
        cin.clear();
        cin.ignore(25,'\n');
        cin >> time1;
    }

    while((time1 > 2400) || (time1 < 0))
    {
        cout << endl;
        cout << " You have entered a wrong time for movie. Enter again!!"<< endl;
        cout << " Enter the time for First show  (HHMM): ";
        cin >> time1;
    }

    cout << endl;
    cout << " Enter the time for Second show (HHMM): ";
    cin >> time2;

     while(!cin >> time2)
    {
        cout << endl;
        cout << " Enter a valid time: " ;
        cin.clear();
        cin.ignore(25,'\n');
        cin >> time2;
    }


    while((time2 > 2400) || (time2 < 0))
    {
        cout << endl;
        cout << " You have entered a wrong time for movie. Enter again!!"<< endl;
        cout << " Enter the time for Second show  (HHMM): ";
        cin >> time2;
    }

    cout << endl;
    cout << " Enter the time for Third show  (HHMM): ";
    cin >> time3;

    while(!cin >> time3)
    {
        cout << endl;
        cout << " Enter a valid time: " ;
        cin.clear();
        cin.ignore(25,'\n');
        cin >> time3;
    }

    while((time3 > 2400) || (time3 < 0))
    {
        cout << endl;
        cout << " You have entered a wrong time for movie. Enter again!!"<< endl;
        cout << " Enter the time for Third show  (HHMM): ";
        cin >> time3;
    }

    movie << name << '|' << time1 << '|' << time2 << '|' << time3;
    cout << endl << endl;
    cout << "You are done, Manager!";
    movie.close();
}
Hello Mathavan,

I did manage to get your code to run. Not sure if what I came up with is what you have, but it works.
I do have a large screen, but the window that the program runs in is not as wide.

This is what my output looks like when I run the program. I hope that is the gao that you are trying for.

********************************************************************************

                     MOVIE TICKET RESERVATION SYSTEM (MTRS)

 ********************************************************************************

========================================================================================================================
====================
|SERIAL NUMBER|                   |            MOVIE NAME                |              |    DAY    |
                  | SHOWTIME AVAILABLE |
========================================================================================================================
====================
 
    (1)                              Avengers                                               MON         
    (1) 0900
    (2) 1000
    (3) 1100
========================================================================================================================
====================



My advice is to create a read function and read the file into a struct and create an array of this struct in "main" that can be passed to different functions.

The way you are going about you have to read the file every time you need it and you are using 2 file streams. 1 to count the lines and 1 to read. This is unnecessary. Also using while (!file.eof()) will loop 1 more time than you expect. I mentioned this once already.

After looking at the above output you should really consider shortening the width of your output unless you know that where the program will be used has a display wide enough for what you did. Not likely though.

Your input file has 4 movies and each movie only has 1 day. This does not go with what you first said.

In your "add function you prompt for " Enter a valid time: " with no example of a time, but you input into an "int" and if a time entered looks like 10:30 you will have a problem. Also note that the input file has leading (0)zeros in the times, but stored in an "int" the leading (0)zeros will be dropped.

My question is do you want to adjust the function "showmovielist" or create a new function maybe based on "showmovielist"?

Andy
Hello Mathavan,

I came up with this for your consideration:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
#include <iomanip>
#include <limits>
#include <string>  // <--- Had to add. Needed for the string class, std::getline and in the "std::cin"c and "std::cout"s/
#include <cctype>  // <--- Added. For "std::tolower() and std::toupper()" + others.
#include <ctime>
#include <cstdlib>

#include <fstream>

constexpr int WIDTH{ 80 };
constexpr int MAXMOVIES{ 10 };

struct Movie
{
    std::string name;
    std::string day;
    int time1H{}, time1M{};
    int time2H{}, time2M{};
    int time3H{}, time3M{};
};

using MOVIE = Movie[MAXMOVIES];

int ReadFile(MOVIE& movies, int& movieCount)
{
    const std::string inFileName{ "Movies.txt" };  // <--- Put File name here.

    char junk{};  // <--- To read the "|"s and ":"s.
    int idx{};

    std::ifstream inFile(inFileName);

    if (!inFile)
    {
        std::cout << "\n File " << std::quoted(inFileName) << " did not open." << std::endl;
        //std::cout << "\n File \"" << inFileName << "\" did not open." << std::endl;

        return 1;
    }

    while (std::getline(inFile, movies[idx].name, '|'))
    {
        std::getline(inFile, movies[idx].day, '|');
        inFile >> movies[idx].time1H >> junk >> movies[idx].time1M >> junk;
        inFile >> movies[idx].time2H >> junk >> movies[idx].time2M >> junk;
        inFile >> movies[idx].time3H >> junk>> movies[idx].time3M;
 
        inFile.ignore();

        idx++;
    }

    movieCount = idx;

    return 0;
}


And the input file is:
Avengers|MON|09:00|10:00|11:00
Superman|MON|09:30|08:00|07:00
Batman|WED|09:00|10:00|11:00
Spiderman|THU|09:30|08:00|07:00
Spaced Invaders|FRI|2:00|4:00|6:00
Dune|FRI|10:00|12:00|2:00
Judge Dred|TUE|4:00|6:00|8:00


The blank line at the bottom is intended. It makes your code in the "showmovielist" function work even though it is wrong.

Using this you create an array of structs with all the information. You only have to read the file once then pass the array around where needed. Much less coding and much easier.

Andy

Edit: Added the "return 0;" to the end of the function.
Last edited on
Hello @Handy Andy. Thanks for helping. I dont know why for you the display is not properly aligned but I have attached how it is on my computer. I appreciate your coding but actually what I'm trying to do is if there is two days allocated for a single movie eg. (If the Spiderman movie will be running in the cinemas om Thursday and also Friday, it should be display in the same box.) I dont want it to be displayed in another box. Hope you can help me with it.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
            ****************************************************************************************

                                            MOVIE TICKET RESERVATION SYSTEM (MTRS)

                   ****************************************************************************************

============================================================================================================================================
|SERIAL NUMBER|                   |            MOVIE NAME                |              |    DAY    |                  | SHOWTIME AVAILABLE |
============================================================================================================================================
    (1)                              Avengers                                               MON                             (1) 0900
                                                                                                                            (2) 1000
                                                                                                                            (3) 1100
============================================================================================================================================
    (2)                              Superman                                               MON                             (1) 0930
                                                                                                                            (2) 0800
                                                                                                                            (3) 0700
============================================================================================================================================
    (3)                              Batman                                                 WED                             (1) 0900
                                                                                                                            (2) 1000
                                                                                                                            (3) 1100
============================================================================================================================================
    (4)                              Spiderman                                              THU                             (1) 0930
                                                                                                                            (2) 0800
                                                                                                                            (3) 0700
============================================================================================================================================

actually what I'm trying to do is if there is two days allocated for a single movie eg. (If the Spiderman movie will be running in the cinemas om Thursday and also Friday, it should be display in the same box.) I dont want it to be displayed in another box.


For proof of concept code, consider as C++20:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <map>
#include <fstream>
#include <string>
#include <vector>
#include <iostream>
#include <sstream>
#include <iomanip>

using Times = std::vector<std::string>;
using Data = std::map<size_t, Times>;
using Films = std::map<std::string, Data>;

struct Movie {
	std::string name;
	size_t day {};
	Times times;
};

constexpr size_t NODAYS {7};
const std::string days[NODAYS + 1] {"SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT", "UKN"};

std::istream& operator>>(std::istream& is, Movie& m)
{
	if (std::string line;  std::getline(is, line)) {
		std::istringstream iss(line);

		std::string day;

		std::getline(iss, m.name, '|');
		std::getline(iss, day, '|');

		const auto itr {std::find(days, days + NODAYS, day)};

		m.day = itr != days + NODAYS ? itr - days : NODAYS;
		m.times.clear();
		for (std::string time; std::getline(iss, time, '|'); m.times.push_back(time));
	}

	return is;
}

int main()
{
	std::ifstream ifs("movies.txt");

	if (!ifs)
		return (std::cout << "Cannot open file\n"), 1;

	Films films;

	for (Movie m; ifs >> m; )
		if (const auto itr {films.find(m.name)}; itr == films.end())
			films.emplace(m.name, Data {{m.day, m.times}});
		else
			itr->second.emplace(m.day, m.times);

	for (size_t movcnt {}; const auto & [name, data] : films) {
		std::ostringstream oss;

		oss << '(' << ++movcnt << ") ";
		std::cout << std::left << std::setw(7) << oss.str()  << std::setw(20) << name;

		for (size_t daycnt {}; const auto & [day, times] : data) {
			if (daycnt++)
				std::cout << std::setw(27) << ' ';

			std::cout << std::setw(10) << days[day];

			for (size_t timcnt {}; const auto & t : times) {
				if (timcnt++)
					std::cout << std::setw(37) << ' ';

				std::cout << '(' << timcnt << ") " << t << '\n';
			}
			std::cout << '\n';
		}
	}
}


Which given movies.txt file as:


Avengers|MON|09:00|10:00|11:00
Superman|MON|09:30|08:00|07:00
Batman|WED|09:00|10:00|11:00
Spiderman|THU|09:30|08:00|07:00
Spaced Invaders|FRI|2:00|4:00|6:00
Dune|FRI|10:00|12:00|2:00
Judge Dred|TUE|4:00|6:00|8:00
Spiderman|FRI|2:00|4:00|6:00


displays:


(1)    Avengers            MON       (1) 09:00
                                     (2) 10:00
                                     (3) 11:00

(2)    Batman              WED       (1) 09:00
                                     (2) 10:00
                                     (3) 11:00

(3)    Dune                FRI       (1) 10:00
                                     (2) 12:00
                                     (3) 2:00

(4)    Judge Dred          TUE       (1) 4:00
                                     (2) 6:00
                                     (3) 8:00

(5)    Spaced Invaders     FRI       (1) 2:00
                                     (2) 4:00
                                     (3) 6:00

(6)    Spiderman           THU       (1) 09:30
                                     (2) 08:00
                                     (3) 07:00

                           FRI       (1) 2:00
                                     (2) 4:00
                                     (3) 6:00

(7)    Superman            MON       (1) 09:30
                                     (2) 08:00
                                     (3) 07:00

Last edited on
Hello Mathavan,


I dont know why for you the display is not properly aligned but I have attached how it is on my computer.



That is the problem not everyone has the same size monitor that you have. My screen may be 23.5 in (58.75cm), but the console window that the program runs in is only 14.5in (36.83cm) which is closet to what a laptop screen might be.

Using the excuse that it works for me does not mean that it will work for everyone. You need to consider where and on what type of system your program might be run on. You have to think ahead a bit and not just write something because it looks good for you.

Given your output:
============================================================================================================================================
|SERIAL NUMBER|                   |            MOVIE NAME                |              |    DAY    |                  | SHOWTIME AVAILABLE |
============================================================================================================================================
     ▲                ▲                                                         ▲           ▲            ▲                      ▲
     1                2                                                         3           4            5                      6
    (1)                              Avengers                                               MON                             (1) 0900
                                                                                                                            (2) 1000
                                                                                                                            (3) 1100
============================================================================================================================================


#1 is taking up more space than is needed. #2, 3 and 5 gives the impression that something is missing. #6 again takes up more space than is needed. #4 is also taking up more space than is needed.

I shortened your output which allowed me to lengthen the block for the name in case you a long movie title. Something you need to think about when you design your program. Should you need room for a name longer than 45 characters there is still a little room to work with.

With what I did I came up with this:

================================================================================
| SERIAL |                                                       |  SHOWTIME  |
| NUMBER |                 MOVIE NAME                    |  DAY  |  AVAILABLE |
 ================================================================================
    (1)    Avengers                                         MON    (1) 09:00
                                                                   (2) 10:00
                                                                   (3) 11:00
    (2)    Avengers                                         WED    (1) 09:00
                                                                   (2) 10:00
                                                                   (3) 11:00
    (3)    Avengers                                         FRI    (1) 09:00
                                                                   (2) 10:00
                                                                   (3) 11:00
 ================================================================================
    (4)    Batman                                           WED    (1) 09:00
                                                                   (2) 10:00
                                                                   (3) 11:00
 ================================================================================
    (5)    Dune                                             FRI    (1) 10:00
                                                                   (2) 12:00
                                                                   (3) 02:00
 ================================================================================
    (6)    Judge Dred                                       TUE    (1) 04:00
                                                                   (2) 06:00
                                                                   (3) 08:00
 ================================================================================
    (7)    Spaced Invaders                                  FRI    (1) 02:00
                                                                   (2) 04:00
                                                                   (3) 06:00
    (8)    Spaced Invaders                                  SAT    (1) 02:00
                                                                   (2) 04:00
                                                                   (3) 06:00
 ================================================================================
    (9)    Spiderman                                        THU    (1) 09:30
                                                                   (2) 08:00
                                                                   (3) 07:00
 ================================================================================
    (10)    Superman                                         MON    (1) 09:30
                                                                   (2) 08:00
                                                                   (3) 07:00
 ================================================================================


I think this is what you want.

This is more compact and easier to read this way.

Now trying to read the file each time you need to make this display would be more work than what you already have done. Unless you read your file and create another that would be in sorted order.

As I showed you with the "RearFile" function. Read the file, put what you read into an array of the structs. If you have studied "vector"s this would be better than an array if not the array is fine. Now all you have to do is pass the array to any function that needs it.

In the "showmovielist" function before I printed out the contents I sorted the array. then in the for loop an if statement would check the names of 2 elements and if they are the same then it would group them together. The only problem you might come across is if the "days" are not in order. For this test it turned out that the "days" are in the right order.

The part I have left to do is to take the "cout" statements in the if/else and put that in a function.

The code I have is in the next post.

Andy
The Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include <iostream>
#include <iomanip>
#include <limits>
#include <string>    // <--- Had to add. Needed for the string class, std::getline and in the "std::cin"s and "std::cout"s.
//#include <cctype>  // <--- Added. For "std::tolower() and std::toupper()" + others. From your previous program.
#include <ctime>
#include <cstdlib>

#include <algorithm>
#include <fstream>

constexpr int WIDTH{ 77 };      // <--- Changed from 80 to 77 for better appearance.
constexpr int MAXMOVIES{ 15 };  // <--- Changed this from 10 to 15 when I added to the input file.

struct Movie
{
    std::string name;
    std::string day;
    int time1H{}, time1M{};  // <--- These could be changed to a "std::string" if you do not do anything with the times.
    int time2H{}, time2M{};
    int time3H{}, time3M{};
};

using MOVIE = Movie[MAXMOVIES];

//  Used by the sort to compart the names of 2 array elements sent by "std::sort".
bool CompareNames(Movie movie1, Movie movie2)
{
    if (movie2.name > movie1.name)
        return true;  // <--- Means to swap.

    return false;  // <--- Means do nothing.
}

int ReadFile(MOVIE& movies, int& movieCount)
{
    const std::string inFileName{ "Movies.txt" };  // <--- Put File name here.

    char junk{};
    int idx{};

    std::ifstream inFile(inFileName);

    if (!inFile)
    {
        std::cout << "\n File " << std::quoted(inFileName) << " did not open." << std::endl;
        //std::cout << "\n File \"" << inFileName << "\" did not open." << std::endl;

        return 1;
    }

    while (std::getline(inFile, movies[idx].name, '|'))
    {
        std::getline(inFile, movies[idx].day, '|');
        inFile >> movies[idx].time1H >> junk >> movies[idx].time1M >> junk;  // <--- If changed to a string you can just use "getline".
        inFile >> movies[idx].time2H >> junk >> movies[idx].time2M >> junk;
        inFile >> movies[idx].time3H >> junk >> movies[idx].time3M;
 
        inFile.ignore();

        idx++;
    }

    movieCount = idx;

    return 0;
}

void title()
{
    const std::string heading{ "MOVIE TICKET RESERVATION SYSTEM (MTRS)" };

    std::cout << "\n\n " << std::string(WIDTH, '*') << "\n\n";
    std::cout << std::string((WIDTH / 2) - (heading.size() / 2), ' ') << heading << "\n\n";
    std::cout << ' ' << std::string(WIDTH, '*') << '\n';

    std::cout << '\n'; // <--- Used for testing. Comment or remove when finished.
}

void showmovielist(MOVIE& movies, const int movieCount)
{
    system("CLS");

    std::ifstream movie("Movies.txt");
    //movie.open("Movies.txt", ios::in);

    int count = 0;
    std::string line;

    title();
    
    std::cout << "\n " << std::string(WIDTH, '=') << "\n";
    std::cout << "| SERIAL |" << std::string(55, ' ') << "|  SHOWTIME  | " << '\n';
    std::cout << "| NUMBER |                 MOVIE NAME                    |  DAY  |  AVAILABLE | " << '\n';
    std::cout << ' ' << std::string(WIDTH, '=') << "\n";

    int j{ 1 };

    //while (i++, i <= count)
    //{
    //    movie.getline(name, 25, '|');
    //    movie.getline(day, 25, '|');
    //    movie.getline(time1, 25, '|');
    //    movie.getline(time2, 25, '|');
    //    movie.getline(time3, 25);
    //    std::string name1 = name;
    //    int name_length = name1.length();

    std::sort(movies, movies + movieCount, CompareNames);

    for (int idx = 0; idx < movieCount; idx++)
    {


        if (movies[idx+1].name == movies[idx].name)
        {
            std::cout <<
                "    (" << idx + 1 << ") " <<
                std::left << std::string(3, ' ') <<
                //std::setfill('*')<< // <--- Used for testing. Comment or remove when finished.
                std::setw(45) << movies[idx].name << "    " <<
                std::setw(7) << movies[idx].day <<
                "(" << j << ") " <<
                (movies[idx].time1H < 10 ? "0" : "") << movies[idx].time1H << ':' << (movies[idx].time1M < 10 ? "0" : "") << movies[idx].time1M << "\n" <<
                std::string(67, ' ') << "(" << j + 1 << ") " <<
                (movies[idx].time2H < 10 ? "0" : "") << movies[idx].time2H << ':' << (movies[idx].time2M < 10 ? "0" : "") << movies[idx].time2M << "\n" <<
                std::string(67, ' ') << "(" << j + 2 << ") " <<
                (movies[idx].time3H < 10 ? "0" : "") << movies[idx].time3H << ':' << (movies[idx].time3M < 10 ? "0" : "") << movies[idx].time3M << "\n";

        }
        else
        {
            std::cout <<
                "    (" << idx + 1 << ") " <<
                std::left << std::string(3, ' ') <<
                //std::setfill('*') << // <--- Used for testing. Comment or remove when finished.
                std::setw(45) << movies[idx].name << "    " <<
                std::setw(7) << movies[idx].day <<
                "(" << j << ") " <<
                (movies[idx].time1H < 10 ? "0" : "") << movies[idx].time1H << ':' << (movies[idx].time1M < 10 ? "0" : "") << movies[idx].time1M << "\n" <<
                std::string(67, ' ') << "(" << j + 1 << ") " <<
                (movies[idx].time2H < 10 ? "0" : "") << movies[idx].time2H << ':' << (movies[idx].time2M < 10 ? "0" : "") << movies[idx].time2M << "\n" <<
                std::string(67, ' ') << "(" << j + 2 << ") " <<
                (movies[idx].time3H < 10 ? "0" : "") << movies[idx].time3H << ':' << (movies[idx].time3M < 10 ? "0" : "") << movies[idx].time3M << "\n";

            std::cout << ' ' << std::string(WIDTH, '=') << "\n";
        }

        j = 1;


    }
}

int main()
{
    int movieCount{};
    int readStatus{};
    MOVIE movies;

    if (readStatus = ReadFile(movies, movieCount))
        return readStatus;

    showmovielist(movies, movieCount);

     return 0;
}


And the input file I used:

Avengers|MON|09:00|10:00|11:00
Superman|MON|09:30|08:00|07:00
Batman|WED|09:00|10:00|11:00
Spiderman|THU|09:30|08:00|07:00
Avengers|WED|09:00|10:00|11:00
Spaced Invaders|FRI|2:00|4:00|6:00
Dune|FRI|10:00|12:00|2:00
Judge Dred|TUE|4:00|6:00|8:00
Spaced Invaders|SAT|2:00|4:00|6:00
Avengers|FRI|09:00|10:00|11:00


Andy

Edit:
Last edited on
This assumes there is 3 showings per day.

You're also showing the movie name multiple times if there are multiple day showings. My understanding is that the movie name is only required to be displayed once ??

If the ordering of the days for multiple day showings are not in order in the file, then the display will also not be in day order - which is probably required.

The (10) Superman line is miss-aligned.

For an 'easier' way of coding this (but uses std::map and std::vector) see my code above and it's output. It doesn't show the grid lines, but that's easily added.

Using this data:


Avengers|MON|09:00|10:00|11:00
Superman|MON|09:30|08:00|07:00
Batman|WED|09:00|10:00|11:00
Spiderman|THU|09:30|08:00|07:00
Avengers|WED|09:00|10:00|11:00
Spaced Invaders|FRI|2:00|4:00|6:00
Dune|FRI|10:00|12:00|2:00
Judge Dred|TUE|4:00|6:00|8:00
Spaced Invaders|SAT|2:00|4:00|6:00
Avengers|FRI|09:00|10:00|11:00
Thor|SAT|4:00|6:00|8:00
Dr Starange|SUN|09:30|08:00|07:00
Captain Marvel|SAT|09:30|08:00|07:00
Thor|WED|09:30|08:00|07:00
Captain America|THU|09:30|08:00|07:00


my code displays:


(1)    Avengers            MON       (1) 09:00
                                     (2) 10:00
                                     (3) 11:00

                           WED       (1) 09:00
                                     (2) 10:00
                                     (3) 11:00

                           FRI       (1) 09:00
                                     (2) 10:00
                                     (3) 11:00

(2)    Batman              WED       (1) 09:00
                                     (2) 10:00
                                     (3) 11:00

(3)    Captain America     THU       (1) 09:30
                                     (2) 08:00
                                     (3) 07:00

(4)    Captain Marvel      SAT       (1) 09:30
                                     (2) 08:00
                                     (3) 07:00

(5)    Dr Starange         SUN       (1) 09:30
                                     (2) 08:00
                                     (3) 07:00

(6)    Dune                FRI       (1) 10:00
                                     (2) 12:00
                                     (3) 2:00

(7)    Judge Dred          TUE       (1) 4:00
                                     (2) 6:00
                                     (3) 8:00

(8)    Spaced Invaders     FRI       (1) 2:00
                                     (2) 4:00
                                     (3) 6:00

                           SAT       (1) 2:00
                                     (2) 4:00
                                     (3) 6:00

(9)    Spiderman           THU       (1) 09:30
                                     (2) 08:00
                                     (3) 07:00

(10)   Superman            MON       (1) 09:30
                                     (2) 08:00
                                     (3) 07:00

(11)   Thor                WED       (1) 09:30
                                     (2) 08:00
                                     (3) 07:00

                           SAT       (1) 4:00
                                     (2) 6:00
                                     (3) 8:00


Last edited on
@ Handy Andy I have modified abitt your coding for me to understand but now I am getting an error in it. I seriously dont know whats the problem with the coding. Can you help me out?
Input

Avengers|MON|0900|1000|1100
Superman|MON|0930|0800|0700
Batman|WED|0900|1000|1100
Spiderman|THU|0930|0800|0700
Avengers|WED|0900|1000|1100
Spaced Invaders|FRI|0200|0400|0600
Dune|FRI|1000|1200|0200
Judge Dred|TUE|0400|0600|0800
Spaced Invaders|SAT|0200|0400|0600
Avengers|FRI|0900|1000|1100


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
struct Movie
{
    string name;
    string day;
    string time1;
    string time2;
    string time3;
};

using MOVIE = Movie[MAXMOVIES];

//  Used by the sort to compart the names of 2 array elements sent by "std::sort".
bool CompareNames(Movie movie1, Movie movie2)
{
    if (movie2.name > movie1.name)
        return true;  // <--- Means to swap.
        return false;  // <--- Means do nothing.
}

int ReadFile(MOVIE& movies, int& movieCount)
{
    string file_name{ "Movies.txt" };  // <--- Put File name here.
    int idx;

    ifstream movies_file(file_name);

    if (!movies_file)
    {
        cout << "\n File " << quoted(file_name) << " did not open." << endl;
        return 1;
    }

    while (getline(movies_file, movies[idx].name, '|'))
    {
        getline(movies_file, movies[idx].day, '|');
        getline(movies_file, movies[idx].time1, '|');
        getline(movies_file, movies[idx].time2, '|');
        getline(movies_file, movies[idx].time3);

        movies_file.ignore();

        idx++;
    }

    movieCount = idx;

    return 0;
}

void showmovielist(MOVIE& movies, const int movieCount)
{
    system("CLS");

    ifstream movie("Movies.txt");

    int count = 0;
    string line;
    title();
    cout << "\n " << string(WIDTH, '=') << "\n";
    cout << "| SERIAL |" << string(55, ' ') << "|  SHOWTIME  | " << '\n';
    cout << "| NUMBER |                 MOVIE NAME                    |  DAY  |  AVAILABLE | " << '\n';
    cout << ' ' << string(WIDTH, '=') << "\n";

    int j = 1;
    int i = 1;

    sort(movies, movies + movieCount, CompareNames);
    int idx;
    for (idx = 0; idx < movieCount; idx++)
    {
        if (movies[idx+1].name == movies[idx].name)
        {
            cout <<
                "    (" << idx + 1 << ") " <<
                std::left << string(3, ' ') <<
                std::setw(45) << movies[idx].name << "    " <<
                std::setw(7) << movies[idx].day <<
                "(" << j << ") " <<
                movies[idx].time1  << "\n" <<
                string(67, ' ') << "(" << j + 1 << ") " <<
                movies[idx].time2 << "\n" <<
                string(67, ' ') << "(" << j + 2 << ") " <<
                movies[idx].time3 << "\n";
        }

        else
        {
            cout <<
                "    (" << idx + 1 << ") " <<
                std::left << string(3, ' ') <<
                std::setw(45) << movies[idx].name << "    " <<
                std::setw(7) << movies[idx].day <<
                "(" << j << ") " <<
                movies[idx].time1 << "\n" <<
                string(67, ' ') << "(" << j + 1 << ") " <<
                movies[idx].time2 << "\n" <<
                string(67, ' ') << "(" << j + 2 << ") " <<
                movies[idx].time3 << "\n";

            cout << ' ' << string(WIDTH, '=') << "\n";

        }
         movie_name[i] = movies[idx].name;
         i = i + 1;
         j = 1;
    }
     counter = idx;
}




 =============================================================================
| SERIAL |                                                       |  SHOWTIME  |
| NUMBER |                 MOVIE NAME                    |  DAY  |  AVAILABLE |
 =============================================================================
    (1)                                                            (1)
                                                                   (2)
                                                                   (3)
 =============================================================================
    (2)    Avengers                                         MON    (1) 0900
                                                                   (2) 1000
                                                                   (3) 1100
 =============================================================================
    (3)    atman                                            WED    (1) 0900
                                                                   (2) 1000
                                                                   (3) 1100
 =============================================================================
    (4)    paced Invaders                                   FRI    (1) 0200
                                                                   (2) 0400
                                                                   (3) 0600
    (5)    paced Invaders                                   SAT    (1) 0200
                                                                   (2) 0400
                                                                   (3) 0600
 =============================================================================
    (6)    piderman                                         THU    (1) 0930
                                                                   (2) 0800
                                                                   (3) 0700
 =============================================================================
    (7)    udge Dred                                        TUE    (1) 0400
                                                                   (2) 0600
                                                                   (3) 0800
 =============================================================================
    (8)    une                                              FRI    (1) 1000
                                                                   (2) 1200
                                                                   (3) 0200
 =============================================================================
    (9)    uperman                                          MON    (1) 0930
                                                                   (2) 0800
                                                                   (3) 0700
 =============================================================================
    (10)    vengers                                          WED    (1) 0900
                                                                   (2) 1000
                                                                   (3) 1100
    (11)    vengers                                          FRI    (1) 0900
                                                                   (2) 1000
                                                                   (3) 1100
 =============================================================================
                   
What is the error ?
What is the error ?


udge Dred
piderman
uperman
Hello Mathavan,

My bad I thought I had put a comment on line 40.

In my code I used formatted input to read the times. You changed that to a "getline" which is fine.

What you need to understand is the difference between formatted and unformatted input.

Formatted input movie_file >> movies[idx].time1; leaves the (\n) in the input buffer. Which is not a problem if followed another formatted input.

Unformatted input, for example, getline(movies_file, movies[idx].time1); will read the whole line up to and including the (\n) which it then discards.

By changing what I did to a "getline" you need the remove line 40 as you now have nothing to ignore, but it is ignoring the first character of the next line.

In the "showmovielist" at the end of the function you have:
1
2
3
4
5
6
7
         movie_name[i] = movies[idx].name;
         i = i + 1;  // <--- Just use i++. Does the same just easier to write.
         j = 1;
    }

     counter = idx;
}

Both "movie_name" and "counter" produced an error as being undefined variables. I have no idea what you are trying to do here.

I do not know if this is what you are trying to achieve, but I did come up with this:

 *****************************************************************************

                   MOVIE TICKET RESERVATION SYSTEM (MTRS)

 *****************************************************************************


 =============================================================================
| SERIAL |                                                       |  SHOWTIME  |
| NUMBER |                 MOVIE NAME                    |  DAY  |  AVAILABLE |
 =============================================================================
   (1)    Avengers                                          MON    (1) 09:00
                                                                   (2) 10:00
                                                                   (3) 11:00

   (2)                                                      WED    (1) 09:00
                                                                   (2) 10:00
                                                                   (3) 11:00

   (3)                                                      FRI    (1) 09:00
                                                                   (2) 10:00
                                                                   (3) 11:00
 =============================================================================
   (4)    Batman                                            WED    (1) 09:00
                                                                   (2) 10:00
                                                                   (3) 11:00
 =============================================================================
   (5)    Dune                                              FRI    (1) 10:00
                                                                   (2) 12:00
                                                                   (3) 02:00
 =============================================================================
   (6)    Judge Dred                                        TUE    (1) 04:00
                                                                   (2) 06:00
                                                                   (3) 08:00
 =============================================================================
   (7)    Spaced Invaders                                   FRI    (1) 02:00
                                                                   (2) 04:00
                                                                   (3) 06:00

   (8)                                                      SAT    (1) 02:00
                                                                   (2) 04:00
                                                                   (3) 06:00
 =============================================================================
   (9)    Spiderman                                         THU    (1) 09:30
                                                                   (2) 08:00
                                                                   (3) 07:00
 =============================================================================
  (10)    Superman                                          MON    (1) 09:30
                                                                   (2) 08:00
                                                                   (3) 07:00
 =============================================================================

First I did some work on the "serial number" to get it to line up the way it does between a 1 digit number and a 2 digit number.

In the inner for loop I used if/else to print the movie name only once. If you do not need this than what you have is fine.

A suggestion I have is since you want to read the times as a string consider the times in the file as "09:00" or even "09:00 AM/PM". And it would not take that much to add a little extra space to the overall width of the display and the "SHOWTIMES AVAILABLE" column.

Andy
Topic archived. No new replies allowed.