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
|
#include <iostream>
#include<string>
#include<fstream>
using namespace std;
void readFromfile(string filename, string title_arr[], int year_arr[], int& numberOfmovies);
void printScreen(string title_arr[], int year_arr[], int numberOfmovies);
void writeToFile(string filename, string title_arr[], int year_arr[], int numberOfmovies);
int main (){
bool done=false;
int menu;
const int MAXSIZE = 100;
string title[MAXSIZE];
int size=0;
int year[MAXSIZE];
while(!done){
cout<<"Select one of the options below\n";
cout<<"1. Read in Movies\n";
cout<<"2. List Titles, years to a screen.\n";
cout<<"3. List Titles, years to a file\n";
cout<<"4. Exit\n";
cin>>menu;
switch(menu){
case 1:
cout<<"Read from file\n";
readFromfile("movies.txt", title, year, size);
break;
case 2:
cout<<"Print to the Screen\n";
printScreen(title, year, size);
break;
case 3:
cout<<"Write to a file\n";
writeToFile("movies_result.txt",title, year, size);
break;
case 4:
cout<<"You will now exit menu.\n";
done=true;
break;
default:
cout<<"Invalid option.\n";
}
}
return 0;
}
void readFromfile(string filename, string title_arr[], int year_arr[], int& numberOfmovies){
string name, y;
int year;
ifstream infile(filename);
while(getline(infile, name)){
if (name.back() =='\r'){
name.pop_back();
}
getline(infile, y);
year=stoi(y);
title_arr[numberOfmovies]=name;
year_arr[numberOfmovies]=year;
cout<<title_arr[numberOfmovies]<<" "<<year_arr[numberOfmovies]<<endl;
numberOfmovies++;
}cout<<endl;
}
void printScreen(string title_arr[], int year_arr[], int numberOfmovies){
cout<<"Total number of movies: "<<numberOfmovies<<endl;
for(int i=0; i<numberOfmovies; i++){
cout<<"Movie Title: "<<title_arr[i]<<"Year: "<<year_arr[i]<<endl;
} cout<<endl;
}
void writeToFile(string filename, string title_arr[], int year_arr[], int numberOfmovies){
ofstream outfile(filename);
outfile<<"Total number of movies: "<<numberOfmovies<<endl;
for(int i=0; i<numberOfmovies;i++){
outfile<<"Movie Title: "<<title_arr[i]<<", year: "<<year_arr[i]<<endl;
}
cout<<endl;
}
|