Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Lab07: int main() { movie movies[100]; int ch; int count = 0; while (true) { cou

ID: 3734348 • Letter: L

Question

Lab07: int main() {
movie movies[100]; int ch; int count = 0; while (true) { cout << "1.Read in Movies "; cout << "2.List Movies to screen "; cout << "3.List Movies to a file "; cout << "4.Exit "; cout << "Enter choice:"; cin >> ch; if (ch == 4) break; if (ch == 1) { count = 0; cout << "Enter filename:"; string name; cin >> name; ifstream fin(name.c_str()); if (!fin) { cout << "Error opening file ";
} while (getline(fin, movies[count].title)) { getline(fin, movies[count].releasingYear); getline(fin, movies[count].runTime); getline(fin, movies[count].rating); count++; } fin.close(); } if (ch == 2) {
for (int i = 0; i<count; i++) { cout << movies[i].title << endl; cout << movies[i].releasingYear << endl; cout << movies[i].runTime << endl; cout << movies[i].rating << endl; } } if (ch == 3) { cout << "Enter filename:"; string name; cin >> name; ofstream fout(name.c_str()); for (int i = 0; i<count; i++) { fout << movies[i].title << endl; fout << movies[i].releasingYear << endl; fout << movies[i].runTime << endl; fout << movies[i].rating << endl; } fout.close(); }
} return 0; } CSCI1411Lab08-Searching and sorting arrays (1) (Protected View)- Word References Mailings Review View Tell me what you want to do Expand on Lab07b, by providing a Search and Sort Function. You should be able to Search or Sort by Title. Write a program that reads in data from a file and places the information in a struct named MovieData. The input file has between zero and 100 "records" each with the all of following tems (in order, separated as noted below) Title ReleaseYear RunningTime Rating might have spaces in them. Running Time is in minutes. Rating is something similar to Title RottenTomatoes.com, so listed in a percent You will have a menu tha t continues until your press exit, and returns an error and goes back to the menu if not one of the choices. You can assume the user will o Page 2 of 3 1. Read in Movies 2. List Movies 3. Search Movies by Title 4. Sort,Movies by Title 5. Exit DOLL

Explanation / Answer

Hello, I have a solution for you. Completed the code to follow required modifications and functionalities to sort and search by title. Comments are included wherever code has been modified, If you have any doubts, feel free to ask, Thanks

//code.cpp

#include<iostream>

#include<fstream>

using namespace std;

//structure for representing one movie data

struct MovieData{

                string title;

                int releasingYear;

                int runTime;

                float rating;

               

};

int main() {

//defining a MovieData array of size 100

MovieData movies[100];

int ch;

int count = 0;

while (true) {

cout << " 1.Read in Movies ";

cout << "2.List Movies to screen ";

cout << "3.List Movies to a file ";

cout << "4.Search Movies by title ";

cout << "5.Sort Movies by title ";

cout << "6.Exit ";

cout << "Enter choice:";

cin >> ch;

if (ch == 6)

break;

if (ch == 1) {

                count = 0;

                cout << "Enter filename:";

                string name;

                cin >> name;

                ifstream fin(name.c_str());

                if (!fin) {

                cout << "Error opening file ";

                }

                char discard;

                /**updated the code to fetch movie details correctly*/

                while (getline(fin, movies[count].title)) {

                                fin>>movies[count].releasingYear;//getting year (int)

                                fin>>movies[count].runTime;//getting run time (int)

                                fin>>movies[count].rating;//getting rating (float)

                                count++;

                                fin.get(discard);//skipping the line break

                }

                fin.close();

                cout<<"movie list has been read!"<<endl;

}

if (ch == 2) {

                /* displaying the movies list*/

                for (int i = 0; i<count; i++) {

                cout<<endl;

                cout << "Name: "<<movies[i].title << endl;

                cout << "Year: "<< movies[i].releasingYear << endl;

                cout << "Run time: "<< movies[i].runTime << endl;

                cout << "Rating: "<< movies[i].rating << endl;

                }

}

if (ch == 3) {

                //writing to file

                cout << "Enter filename:";

                string name;

                cin >> name;

                ofstream fout(name.c_str());

                for (int i = 0; i<count; i++) {

                                fout << movies[i].title << endl;

                                fout << movies[i].releasingYear << endl;

                                fout << movies[i].runTime << endl;

                                fout << movies[i].rating << endl;

                }

                fout.close();

                cout<<"movie list has been saved to the file!"<<endl;

}

if(ch==4){

                //searching by title

                cin.ignore();//skips the next line char

                cout<<"Enter title: "<<endl;

                string title;

                getline(cin,title);//getting title

                cout<<"Results: "<<endl;

                for(int i=0;i<count;i++){

                                if(movies[i].title.find(title)!=string::npos){

                                                //found

                                                cout<<endl;

                                                cout << "Name: "<<movies[i].title << endl;

                                                cout << "Year: "<< movies[i].releasingYear << endl;

                                                cout << "Run time: "<< movies[i].runTime << endl;

                                                cout << "Rating: "<< movies[i].rating << endl;

                                }

                }

}

if(ch==5){

                //sorting by title

                for(int i=0;i<count;i++){

                                for(int j=0;j<i;j++){

                                                if(movies[j].title>movies[i].title){

                                                                //swapping data

                                                                MovieData temp=movies[j];

                                                                movies[j]=movies[i];

                                                                movies[i]=temp;

                                                }

                                }

                }

                cout<<"Sorted successfully!"<<endl;

}

}

return 0;

}

//movies.txt

Shawshank Redemption

1994

100

92

Godfather

1972

120

91

Pulp Fiction

1994

120

89

Inception

2010

90

94

The Dark Knight

2008

110

85

Fight Club

1999

105

87

Toy Story 3

2010

80

80

Fargo

1996

135

80

/*OUTPUT*/

1.Read in Movies

2.List Movies to screen

3.List Movies to a file

4.Search Movies by title

5.Sort Movies by title

6.Exit

Enter choice:1

Enter filename:movies.txt

1.Read in Movies

2.List Movies to screen

3.List Movies to a file

4.Search Movies by title

5.Sort Movies by title

6.Exit

Enter choice:2

Name: Shawshank Redemption

Year: 1994

Run time: 100

Rating: 92

Name: Godfather

Year: 1972

Run time: 120

Rating: 91

Name: Pulp Fiction

Year: 1994

Run time: 120

Rating: 89

Name: Inception

Year: 2010

Run time: 90

Rating: 94

Name: The Dark Knight

Year: 2008

Run time: 110

Rating: 85

Name: Fight Club

Year: 1999

Run time: 105

Rating: 87

Name: Toy Story 3

Year: 2010

Run time: 80

Rating: 80

Name: Fargo

Year: 1996

Run time: 135

Rating: 80

1.Read in Movies

2.List Movies to screen

3.List Movies to a file

4.Search Movies by title

5.Sort Movies by title

6.Exit

Enter choice:5

Sorted successfully!

1.Read in Movies

2.List Movies to screen

3.List Movies to a file

4.Search Movies by title

5.Sort Movies by title

6.Exit

Enter choice:2

Name: Fargo

Year: 1996

Run time: 135

Rating: 80

Name: Fight Club

Year: 1999

Run time: 105

Rating: 87

Name: Godfather

Year: 1972

Run time: 120

Rating: 91

Name: Inception

Year: 2010

Run time: 90

Rating: 94

Name: Pulp Fiction

Year: 1994

Run time: 120

Rating: 89

Name: Shawshank Redemption

Year: 1994

Run time: 100

Rating: 92

Name: The Dark Knight

Year: 2008

Run time: 110

Rating: 85

Name: Toy Story 3

Year: 2010

Run time: 80

Rating: 80

1.Read in Movies

2.List Movies to screen

3.List Movies to a file

4.Search Movies by title

5.Sort Movies by title

6.Exit

Enter choice:4

Enter title:

Fargo

Results:

Name: Fargo

Year: 1996

Run time: 135

Rating: 80

1.Read in Movies

2.List Movies to screen

3.List Movies to a file

4.Search Movies by title

5.Sort Movies by title

6.Exit

Enter choice:3

Enter filename:movies2.txt

movie list has been saved to the file!

1.Read in Movies

2.List Movies to screen

3.List Movies to a file

4.Search Movies by title

5.Sort Movies by title

6.Exit

Enter choice:6