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;
}
|