49.6k views
0 votes
TV show information can either keep track of the number of viewers per show or the name of a show. Information is kept in a file (see sample files below). If the first line of the file says Show, we should store the names of the shows. If the first line of the file says Viewer, we should store the number of viewers per show.

Create a class to complete the program below.
int main(int argc, char** argv) Tv_show_info Tv_show_info show_names(argv[2]);
int n=viewers.info.at(0);
std::string s=show_names.info.at(0);

Sample input file 2: Viewer Sample input file 1: Show Stranger Things Game of Thrones Black Mirror 34 100

its not provided, we are to recreate it. but this is what i made..

template
class Tv_show_info
{
public:
// template
Tv_show_info(string & file):
filename{file}
{
ifstream myfile {filename};
vectorinfo;
T lines;
while (getline(myfile,lines))
{
info.push_back(lines);
}
}
private:
string filename;
};

1 Answer

6 votes

Answer:

See explaination for Program source code.

Step-by-step explanation:

The program source code below.

#include <iostream>

#include <fstream>

#include <vector>

using namespace std;

class Show

{

private:

string title;

int viewers;

public:

Show()

{

this->title = "";

this->viewers = 0;

}

Show(string title, int viewers)

{

this->title = title;

this->viewers = viewers;

}

string getTitle(){ return this->title; }

int getViewers(){ return this->viewers; }

};

int main()

{

vector<Show> shows;

vector<string> titles;

vector<int> viewers;

ifstream inFile1("file1.txt");

ifstream inFile2("file2.txt");

string line1, line2;

if(!inFile1.is_open() || !inFile2.is_open())

{

cout << "Either of the files could not be found!\\";

exit(0);

}

getline(inFile1, line1);

if(line1.compare("Show") == 0)

{

while(getline(inFile1, line1))

{

titles.push_back(line1);

}

inFile1.close();

}

getline(inFile2, line2);

if(line2.compare("Viewer") == 0)

{

while(getline(inFile2, line2))

{

viewers.push_back(stod(line2));

}

inFile2.close();

}

for(int i = 0; i < titles.size(); i++)

{

shows.push_back(Show(titles[i], viewers[i]));

}

// display all the show details

cout << "\\ALL SHOWS:\\----------\\";

for(Show show : shows)

{

cout << "Title: " << show.getTitle() << ", Number of viewers: " << show.getViewers() << endl;

}

cout << endl;

return 0;

}

See attachment for output

TV show information can either keep track of the number of viewers per show or the-example-1
User Robert Raboud
by
4.3k points