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
|
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;
struct Movie
{
int timesCalled;
int year;
string name;
string director;
};
Movie *readMovieInfo(int &size);
int binarySearch(Movie *array, int size, string value);
void printResults(int result, int size, Movie *array);
int main()
{
int size, result = 0;
string value;
Movie *movieList = {0}; // Initialize timesCalled to 0
movieList = readMovieInfo(size);
for (int i = 0; i < size; i++)
{
cout << movieList[i].timesCalled << endl;
}
cout << "Here are the available directors:" << endl << endl;
for (int i = 1; i < size; i++)
{
cout << left << setw(20) << movieList[i - 1].director << "\t";
if (i % 3 == 0)
cout << endl;
}
cout << endl << "Enter a director's name: ";
getline(cin, value);
result = binarySearch(movieList, size, value);
printResults(result, size, movieList);
/*
for (int i = 0; i < size; i++)
{
cout << movieList[i].year << " ";
cout << movieList[i].name << " ";
cout << movieList[i].director << endl;
}
*/
return 0;
}
Movie *readMovieInfo(int &size)
{
ifstream inFile;
string file;
Movie *movieList;
cout << "What is the name of the file you would like to open? ";
getline(cin, file);
file.append(".txt");
inFile.open(file.c_str());
if(!inFile) // File validation
{
cout << "Can't open the input file!" << endl;
exit(111);
}
inFile >> size;
movieList = new Movie[size];
for (int i = 0; i < size; i++)
{
inFile >> movieList[i].year;
getline(inFile, movieList[i].name, '\"');
getline(inFile, movieList[i].name, '\"');
//movieList[i].name = movieList[i].name;
getline(inFile, movieList[i].director, '\"');
getline(inFile, movieList[i].director, '\"');
//movieList[i].director = movieList[i].director;
}
inFile.close();
return movieList;
}
|