I need help coding this program in C++ !! This is the text to be sorted (It incl
ID: 3910111 • Letter: I
Question
I need help coding this program in C++ !!
This is the text to be sorted (It includes the equal signs):
Darby O'Gill and the Little People
1959
RT 93 minutes
Albert Sharp
Janet
Munro
Sean Connery
Jimmy O'Dea
Kieron
Moore
Estelle
Winwood
Walter Fitzgerald
=================================================
Dr. No
1962
RT 109 minutes
Sean Connery
Ursula Andress
Bernard Lee
=================================================
From Russia with Love
1963
RT 115 minutes
Sean Connery
Daniela Bianchi
Pedro Armendariz
Lotte Lenya
Robert Shaw
Bernard Lee
=================================================
Goldfinger
1964
RT 110 minutes
Sean Connery
Gert Frobe
Honor Blackman
=================================================
You Only Live Twice
1967
RT 117 minutes
Sean Connery
Akiko Wakabayashi
Mie Hama
=================================================
Diamonds Are Forever
1971
Sean Connery
Jill St. John
Charles Gray
=================================================
The Untouchables
1987
Kevin Costner
Sean Connery
Robert De Niro
=================================================
Indiana Jones and the Last Crusade
1989
Harrison Ford
Sean Connery
Alison Doody
=================================================
The Hunt for Red October
1990
RT 135 minutes
Sean Connery
Alec Baldwin
Scott Glenn
Sam Neill
James Earl Jones
Explanation / Answer
Given below is the code for the question.
Please do rate the answer if it was helpful. Thank you
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
struct star{
string firstname;
string lastname;
};
struct movie{
string name;
int releaseYear;
int runningTime; //minutes
int numStars;
star stars[10];
};
void sortMoviesByName(movie movies[], int size);
void sortStarsByName(star stars[], int size);
int readFile(string filename, movie movies[]);
void printMovies(movie movies[], int size);
int main(){
int size = 0;
string inFilename;
movie movies[50];
cout << "Enter input filename: ";
cin >> inFilename;
size = readFile(inFilename, movies);
//printMovies(movies, size);
sortMoviesByName(movies, size);
for(int i = 0; i < size; i++)
{
sortStarsByName(movies[i].stars, movies[i].numStars);
}
printMovies(movies, size);
}
void sortMoviesByName(movie movies[], int size){
int i, j, minIdx;
movie temp;
for(i = 0; i < size; i++)
{
minIdx = i;
for(j = i +1; j < size; j++)
{
if(movies[j].name < movies[minIdx].name)
minIdx = j;
}
if(minIdx != i)
{
temp = movies[minIdx];
movies[minIdx] = movies[i];
movies[i] = temp;
}
}
}
void sortStarsByName(star stars[], int size){
int i, j, minIdx;
star temp;
for(i = 0; i < size; i++)
{
minIdx = i;
for(j = i +1; j < size; j++)
{
if(stars[j].lastname < stars[minIdx].lastname)
minIdx = j;
}
if(minIdx != i)
{
temp = stars[minIdx];
stars[minIdx] = stars[i];
stars[i] = temp;
}
}
}
int readFile(string filename, movie movies[]){
ifstream in(filename.c_str());
if(in.fail()){
cout << "ERROR: could not read input file " << filename << endl;
exit(1);
}
int n = 0;
string s, first, last;
string line;
while(getline(in, movies[n].name)){
//cout << movies[n].name << endl;
in >> movies[n].releaseYear;
movies[n].numStars = 0;
in >> s;
if(s == "RT") //runtime is present
{
in >> movies[n].runningTime; //get running time
in.ignore(256, ' '); //ignroe rest of line till newline
in >> s;
}
else
movies[n].runningTime = 0;
int i = 0;
while(s[i] != '=')
{
first = s;
getline(in, last);
movies[n].stars[i].firstname = first;
movies[n].stars[i].lastname= last;
i++;
in >> s;
if(in.eof())
break;
}
movies[n].numStars = i;
in.ignore(256, ' ');
n++;
}
in.close();
return n;
}
void printMovies(movie movies[], int size){
for(int i = 0; i < size; i++){
cout << movies[i].name << endl;
cout << movies[i].releaseYear << endl;
if(movies[i].runningTime != 0)
cout << "RT " << movies[i].runningTime << " minutes" << endl;
for(int j = 0; j < movies[i].numStars; j++)
cout << movies[i].stars[j].firstname << " " << movies[i].stars[j].lastname << endl;
cout << "======================" << endl;
}
cout << endl;
}
output
=====
Enter input filename: movies.txt
Darby O'Gill and the Little People
1959
RT 93 minutes
Sean Connery
Walter Fitzgerald
Kieron Moore
Janet Munro
Jimmy O'Dea
Albert Sharp
Estelle Winwood
======================
Diamonds Are Forever
1971
Sean Connery
Charles Gray
Jill St. John
======================
Dr. No
1962
RT 109 minutes
Ursula Andress
Sean Connery
Bernard Lee
======================
From Russia with Love
1963
RT 115 minutes
Pedro Armendariz
Daniela Bianchi
Sean Connery
Bernard Lee
Lotte Lenya
Robert Shaw
======================
Goldfinger
1964
RT 110 minutes
Honor Blackman
Sean Connery
Gert Frobe
======================
Indiana Jones and the Last Crusade
1989
Sean Connery
Alison Doody
Harrison Ford
======================
The Hunt for Red October
1990
RT 135 minutes
Alec Baldwin
Sean Connery
James Earl Jones
Scott Glenn
Sam Neill
======================
The Untouchables
1987
Sean Connery
Kevin Costner
Robert De Niro
======================
You Only Live Twice
1967
RT 117 minutes
Sean Connery
Mie Hama
Akiko Wakabayashi
======================
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.