In C++. I\'m having particular trouble with this C++ problem. In this program, t
ID: 640190 • Letter: I
Question
In C++. I'm having particular trouble with this C++ problem. In this program, the menu is to pop up, asking the user to select a movie genre from the list (which is read into an array from a text file). From this choice, the user is shown a movie listed from another text file (read into another array). It sounds simple, but I'm having much difficulty in making things work. Here's what I've done. PLEASE HELP!
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
int menu(string*,int)
{
char ch;
cout<<"Enter your choice:"<<endl;
cout<<"(R)ecommend Movie"<<endl;
cout<<"or"<<endl;
cout<<"(Q)uit"<<endl;
cout<<"CHOICE: "<<endl;
cin>>ch;
switch(ch)
{
case 'r':
case 'R':loadGenres(int*);
nextMenu();
break;
case 'q':
case 'Q':cout<<"Thank you for using our recommendations. Enjoy your movie."<<endl;
system("PAUSE");
return 0;
default: cout<<"Invalid Entry. Please enter a valid option."<<endl;
break;
}
}
void nextMenu()
{
int movieSelection;
cout<<"Here are the genres to choose from: "<<endl
for (int j=0;j<50;j++)
{
cout<<j<<". "<<genresList[j]<<endl;
}
cout<<"Please enter your selection, or enter -1 to end"<<endl;
cin>>movieSelection;
while (movieSelection!=-1)
{
cout<<"You entered "<<movieSelection<<". "<<endl;
string* searchMovie(string* pMovies, int nMovies, string genre, int *returned count);
}
if (movieSelection==-1)
{
return;
}
}
string* loadGenres(int*)
{
string genresList[50];
ifstream inputFile;
inputFile.open("genres.txt");
for (int z=0; z<50; z++)
{
inputFile >> genresList[50];
}
inputFile.close();
loadMovies();
}
string* loadMovies(int*)
{
string moviesList[12000];
ifstream inputFile;
inputFile.open("movies.txt");
for (int y=0; y<50; y++)
{
inputFile >> moviesList[12000];
}
inputFile.close();
sortData();
}
void sortData(string*, int)
{
bool swap;
int temp,array,size;
do
{
swap=false;
for (int count=0; count<(size-1);count++)
{
temp=array[count];
array[count]=array[count+1];
array[count+1]=temp;
swap=true;
}
}
while (swap);
}
string* searchMovie(string* pMovies, int nMovies, string genre, int *returned count)
{
string g = "one";
string h = "one ";
string tmpG = g;
tmpG.resize(h.size(), ' ');
if (tmpG == h) {
cout << "4. Same (RIGHT) ";
} else {
cout << "4. Different (WRONG) ";
}
srand(time(0));
}
string selectMovie()
{
cout<<"We recommend the following movies: "<<endl;
}
main()
{
cout<<"Movie Recommendation Selector"<<endl;
cout<<" "<<endl;
menu();
}
Explanation / Answer
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include<ctime>
#include<cstdlib>
using namespace std;
string genresList[50];
string moviesList[150];
void sortData(string array[], int size)
{
bool swap;
string temp;
do
{
swap=false;
for (int count=0; count<(size-1);count++)
{
if(array[count]>array[count+1])
{
temp=array[count];
array[count]=array[count+1];
array[count+1]=temp;
swap=true;
}
}
}
while (swap);
}
void loadMovies()
{
ifstream inputFile;
inputFile.open("movies.txt");
int y=0;
while(!inputFile.eof())
{
inputFile >> moviesList[y++];
}
inputFile.close();
sortData(moviesList,y);
}
void nextMenu(int size)
{
int movieSelection;
cout<<"Here are the genres to choose from: "<<endl;
for (int j=0;j<size;j++)
{
cout<<j+1<<". "<<genresList[j]<<endl;
}
cout<<"Please enter your selection, or enter -1 to end: ";
cin>>movieSelection;
if (movieSelection==-1)
{
return;
}
else
{
cout<<"You entered "<<movieSelection<<". "<<endl;
string* searchMovie(string* pMovies, int nMovies, string genre, int *returned_count);
}
}
int loadGenres()
{
ifstream inputFile;
inputFile.open("genres.txt");
int y=0;
while(!inputFile.eof())
{
inputFile >> genresList[y++];
}
inputFile.close();
loadMovies();
return y;
}
string* searchMovie(string* pMovies, int nMovies, string genre, int *returned_count)
{
string g = "one";
string h = "one ";
string tmpG = g;
tmpG.resize(h.size(), ' ');
if (tmpG == h) {
cout << "4. Same (RIGHT) ";
} else {
cout << "4. Different (WRONG) ";
}
srand(time(0));
}
string selectMovie()
{
cout<<"We recommend the following movies: "<<endl;
}
void menu()
{
char ch;
int size;
cout<<"Enter your choice:"<<endl;
cout<<"(R)ecommend Movie"<<endl;
cout<<"or"<<endl;
cout<<"(Q)uit"<<endl;
cout<<"CHOICE: ";
cin>>ch;
switch(ch)
{
case 'r':
case 'R':size=loadGenres();
nextMenu(size);
break;
case 'q':
case 'Q':cout<<"Thank you for using our recommendations. Enjoy your movie."<<endl;
system("PAUSE");
return;
default: cout<<"Invalid Entry. Please enter a valid option."<<endl;
break;
}
}
int main()
{
cout<<"Movie Recommendation Selector"<<endl;
cout<<" "<<endl;
menu();
return 0;
}