moviestruct.cpp
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <ostream>
#include <fstream>
#include <cstdlib>
#include <cstring>
using namespace std;
typedef struct{
int id;
char title[250];
int year;
char rating[6];
int totalCopies;
int rentedCopies;
}movie;
int loadData(ifstream &infile, movie movies[]);
void printAll(movie movies[], int count);
void printRated(movie movies[], int count);
void printTitled(movie movies[], int count);
void addMovie(movie movies[],int &count);
void returnMovie(movie movies[],int count);
void rentMovie(movie movies[],int count);
void saveToFile(movie movies[], int count, char *filename);
void printMovie(movie &m);
int find(movie movies[], int count, int id);
int main(int argc, char *argv[])
{
//open file for catalog.txt
char filename[100] = "catalog.txt";
ifstream infile(filename);
movie movies[200];
int count = 0;
if(infile.is_open())
{
count = loadData(infile, movies);
}
int choice = 0;
while(choice != 7)
{
cout << " ********** MAIN MENU ********** " << endl;
cout << "1 - Print Calalog" << endl;
cout << "2 - Search by Title" << endl;
cout << "3 - Search by Rating" << endl;
cout << "4 - Add Movie" << endl;
cout << "5 - Rent Movie" << endl;
cout << "6 - Return Movie" << endl;
cout << "7 - Quit" << endl;
cout << "Enter choice: ";
cin >> choice; //switch statements for different user choices
switch(choice)
{
case 1:
printAll(movies, count);
break;
case 2:
printTitled(movies, count);
break;
case 3:
printRated(movies, count);
break;
case 4:
addMovie(movies, count);
break;
case 5:
rentMovie(movies, count);
break;
case 6:
returnMovie(movies, count);
break;
case 7:
saveToFile(movies, count, filename);
break;
default:
cout << "Invalid choice. Please re-enter: " << endl;
}
}
return 0;
}
int loadData(ifstream &infile, movie movies[])
{
int count = 0;
while(infile >> movies[count].id)
{
infile.ignore();
infile.getline(movies[count].title, 250);
infile >> movies[count].year;
infile >> movies[count].rating;
infile >> movies[count].totalCopies;
infile >> movies[count].rentedCopies;
count++;
}
infile.close();
return count;
}
void printMovie(movie &m)
{
cout << "---------- ID: " << m.id << " ----------" << endl;
cout << "Title: " << m.title << endl;
cout << "Year: " << m.year << endl;
cout << "Rating: " << m.rating << endl;
cout << "Number of copies: " << m.totalCopies << endl;
cout << "Number rented: " << m.rentedCopies << endl;
cout << "---------------------------------------" << endl;
}
void printAll(movie movies[], int count)
{
cout << "%%%%%%%%%% Movie Catalog %%%%%%%%%%" << endl;
for(int i = 0 ;i < count; i++)
{
printMovie(movies[i]);
}
cout << endl;
}
void printRated(movie movies[], int count)
{
int r = -1;
char rating[5] = "";
cout << "0 - NONE" << endl;
cout << "1 - G" << endl;
cout << "2 - PG" << endl;
cout << "3 - PG13" << endl;
cout << "4 - R" << endl;
cout << "5 - N17" << endl;
while(true)
{
cin >> r;
if(r < 0 || r > 5)
cout << "Invalid choice. Please re-enter: ";
else
break;
}
if(r == 0)
strcpy(rating, "NONE");
else if(r == 1)
strcpy(rating, "G");
else if(r == 2)
strcpy(rating, "PG");
else if(r == 3)
strcpy(rating, "PG13");
else if(r == 4)
strcpy(rating, "R");
else if(r == 5)
strcpy(rating, "N17");
for(int i = 0 ;i < count; i++)
{
if(strcmp(movies[i].rating, rating) == 0)
printMovie(movies[i]);
}
cout << endl;
}
void printTitled(movie movies[], int count)
{
char title[250];
cout << "Title of Movie? " << endl;
cin.ignore(); //flush newline
cin.getline(title, 250);
cout << endl;
for(int i = 0 ;i < count; i++)
{
if(strcmp(movies[i].title, title) == 0)
{
printMovie(movies[i]);
break;
}
}
cout << endl;
}
int find(movie movies[], int count, int id)
{
for(int i = 0; i < count; i++)
{
if(movies[i].id == id)
return i;
}
return -1;
}
void addMovie(movie movies[],int &count) //pass by ref
{
int id;
cout << "Moview ID? (must be unique) " ;
cin >> id;
while(find(movies, count, id) != -1)
{
cout << "That ID is already in use. Please enter another: ";
cin >> id;
}
movies[count].id = id;
cin.ignore();
cout << "Title? ";
cin.getline(movies[count].title, 250);
cout << "Year? " ;
cin >> movies[count].year;
cout << "Movie rating? " << endl;
int r = -1;
cout << "0 - NONE" << endl;
cout << "1 - G" << endl;
cout << "2 - PG" << endl;
cout << "3 - PG13" << endl;
cout << "4 - R" << endl;
cout << "5 - N17" << endl;
while(true)
{
cin >> r;
if(r < 0 || r > 5)
cout << "Invalid choice. Please re-enter: ";
else
break;
}
if(r == 0)
strcpy(movies[count].rating, "NONE");
else if(r == 1)
strcpy(movies[count].rating, "G");
else if(r == 2)
strcpy(movies[count].rating, "PG");
else if(r == 3)
strcpy(movies[count].rating, "PG13");
else if(r == 4)
strcpy(movies[count].rating, "R");
else if(r == 5)
strcpy(movies[count].rating, "N17");
cout << "Copies? " ;
while(true)
{
cin >> movies[count].totalCopies;
if(movies[count].totalCopies <= 0 )
cout << "Copies must not be negative. Please re-enter: " ;
else
break;
}
movies[count].rentedCopies = 0;
count++;
}
void returnMovie(movie movies[],int count)
{
char answer;
cout << "Do you want to see a list of movies first? (y or n) ";
cin >> answer;
if(answer == 'y' || answer == 'Y')
printAll(movies, count);
int id;
cout << "Enter id of movie: ";
while(true)
{
cin >> id;
if(id == -1)
break;
int index = find(movies, count, id);
if(index == -1)
cout << "The movie does not exist. Please re-enter(or enter -1 to quit): " << endl;
else
{
if(movies[index].rentedCopies == 0)
cout << "No copies were rented." << endl;
else
{
movies[index].rentedCopies--;
cout << movies[index].title << " returned successfully. " << endl;
}
break;
}
}
}
void rentMovie(movie movies[],int count)
{
char answer;
cout << "Do you want to see a list of movies first? (y or n) ";
cin >> answer;
if(answer == 'y' || answer == 'Y')
printAll(movies, count);
int id;
cout << "Enter id of movie: ";
while(true)
{
cin >> id;
if(id == -1)
break;
int index = find(movies, count, id);
if(index == -1)
cout << "The movie does not exist. Please re-enter(or enter -1 to quit): " << endl;
else
{
if(movies[index].rentedCopies == movies[index].totalCopies)
cout << "There are no copies to rent." << endl;
else
{
movies[index].rentedCopies++;
cout << "One copy of " << movies[index].title << " rented. " << endl;
}
break;
}
}
}
void saveToFile(movie movies[], int count, char *filename)
{
ofstream outfile(filename);
if(!outfile.is_open())
cout << "Error writing to output file " << filename << endl;
else
{
for(int i = 0; i < count; i++)
{
outfile << movies[i].id << endl;
outfile << movies[i].title << endl;
outfile << movies[i].year << endl;
outfile << movies[i].rating << endl;
outfile << movies[i].totalCopies << endl;
outfile << movies[i].rentedCopies << endl;
}
outfile.close();
}
}
catalog.txt
10
Singing in the Rain
1952
NONE
10
1
20
Star Wars
1977
PG
20
7
30
Wonder Woman
2017
PG13
10
0
40
The Mummy
1999
PG13
9
3
50
The Shawshank Redemption
1994
R
3
1
60
The Mummy
2017
PG13
4
0
70
Cars
2006
G
6
0
80
Finding Nemo
2003
G
9
5
90
The moviestruct.cpp already works fine with catalog.txt. Just need to change moviestruct.cpp to meet the requirements and need a catalog class that works with it.
//Screenshot of the code:
//Movie.h
//Movie.cpp
//Catalog.h
//Catalog.cpp
//main.h
//main.cpp
//Sample Output:
//Sample Output File:
//Catalog.txt
10
Singing in the Rain
1952
NONE
10
1
120
Adam Eve
1990
G
20
10
30
Wonder Woman
2017
PG13
10
0
40
The Mummy
1999
PG13
9
3
50
The Shawshank Redemption
1994
R
3
1
60
The Mummy
2017
PG13
4
0
70
Cars
2006
G
6
0
130
Captain Nemo
2020
N17
100
8
100
3 idiots
2009
PG
30
20
200
Insidious
2013
R
30
10
//Code to copy:
//Movie.h
//Include the required header files.
#include "stdafx.h"
#include <iostream>
//Use the standard naming convention.
using namespace std;
//Define the class movie.
class Movie
{
//Declare the required public member functions of the
//class.
public:
int getId();
char* getTitle();
int getYear();
char* getRating();
int getCopies();
int getRentedCopies();
void setId(int id);
void setTitle(char name[]);
void setYear(int year);
void setRating(char rate[]);
void setCopies(int totalCopies);
void setRentedCopies(int rentedCopies);
//Declare the required private member variables of the
//class.
private:
int id;
char title[250];
int year;
char rating[6];
int totalCopies;
int rentedCopies;
};
//Movie.cpp
//Declare the required header files.
#include "stdafx.h"
#include <iostream>
#include "Movie.h"
//Use the standard naming convention.
using namespace std;
//Define the function to get the id of the movie.
int Movie::getId()
{
return id;
}
//Define the function to get the title of the movie.
char* Movie::getTitle()
{
return title;
}
//Define the function to get the year of the movie.
int Movie::getYear()
{
return year;
}
//Define the function to get the rating of the movie.
char* Movie::getRating()
{
return rating;
}
//Define the function to get the total copies of the
movie.
int Movie::getCopies()
{
return totalCopies;
}
//Define the function to get the rented copies of the
//movie.
int Movie::getRentedCopies()
{
return rentedCopies;
}
//Define the function to set the id of the movie.
void Movie::setId(int mid)
{
id = mid;
}
//Define the function to set the title of the movie.
void Movie::setTitle(char name[])
{
int i;
for (i = 0; name[i] != '\0'; i++)
{
title[i] = name[i];
}
title[i] = '\0';
}
//Define the function to set the year of the movie.
void Movie::setYear(int myear)
{
year = myear;
}
//Define the function to set the rating of the movie.
void Movie::setRating(char rate[])
{
int i;
for (i = 0; rate[i] != '\0'; i++)
{
rating[i] = rate[i];
}
rating[i] = '\0';
}
//Define the function to set the total copies of the
movie.
void Movie::setCopies(int mCopies)
{
totalCopies = mCopies;
}
//Define the function to set the rented copies of the
//movie.
void Movie::setRentedCopies(int mRentedCopies)
{
rentedCopies = mRentedCopies;
}
//Catalog.h
//Declare the required header files.
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <ostream>
#include <cstring>
#include "Movie.h"
//Use the standard naming convention.
using namespace std;
//Define the class Catalog inheriting the movie class.
class Catalog :public Movie
{
//Declare the array of objects.
Movie movie[256];
//Declare the required variables.
int count;
//Declare the required public member variables and member
//functions of the class.
public:
int r;
int id;
char title[250];
int year;
char rating[6];
int totalCopies;
int rentedCopies;
void readFile();
int loadData(ifstream &infile);
void printAll();
void printMovie(Movie &m);
void printTitled();
void printRated();
int find(Movie movie[], int count, int id);
void addMovie();
void modifyMovie();
void rentMovie();
void returnMovie();
void saveToFile(char *filename);
};
//Catalog.cpp
//Include the required header files.
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <ostream>
#include <string>
#include "main.h"
//Use the standard naming convention.
using namespace std;
//Define the function to read the input file.
void Catalog::readFile()
{
//Open the required file.
char filename[100] = "Catalog.txt";
ifstream infile(filename);
//Check if the file is present or not.
if (!infile.is_open())
{
cout << "File is not present" <<
endl;
count = 0;
return;
}
else
{
//Call the loadData function to load the data
//of the file and store the count.
count = loadData(infile);
}
}
//Define the function loadData to load the content
of //the file.
int Catalog::loadData(ifstream &infile)
{
//Initialize the count variable.
count = 0;
//Load the content of the file into the respective
//fields of the movie class till the last id in
the //file.
while (infile >> id)
{
movie[count].setId(id);
infile.ignore(26, '\n');
infile.getline(title, 250);
movie[count].setTitle(title);
infile >> year;
movie[count].setYear(year);
infile.ignore(26, '\n');
infile.getline(rating, 6);
movie[count].setRating(rating);
infile >> totalCopies;
movie[count].setCopies(totalCopies);
infile >> rentedCopies;
movie[count].setRentedCopies(rentedCopies);
//Increment the count in each iteration.
count++;
}
//Close the file.
infile.close();
//Return the count of the file.
return count;
}
//Define the function printAll to display the contents of //the
file.
void Catalog::printAll()
{
cout << "%%%%%%%%%% Movie Catalog %%%%%%%%%%";
cout << endl;
for (int i = 0; i < count; i++)
{
//Call the function printMovie to print the
//details of each movie.
printMovie(movie[i]);
}
cout << endl;
}
//Define the function printMovie to display the details of
//each movie in the file.
void Catalog::printMovie(Movie &m)
{
cout << "---------- ID: " << m.getId();
cout << " ----------" << endl;
cout << "Title: " << m.getTitle() << endl;
cout << "Year: " << m.getYear() << endl;
cout << "Rating: " << m.getRating() <<
endl;
cout << "Number of copies: " << m.getCopies();
cout << endl;
cout << "Number rented: " << m.getRentedCopies();
cout << endl;
cout << "-------------------------------------";
cout << "--" << endl;
}
//Define the function to display the movies by their
title.
void Catalog::printTitled()
{
//Prompt the user to input the title of the
//movie.
char title[250];
cout << "Title of Movie? " << endl;
cin.ignore(); //flush newline
cin.getline(title, 250);
for (int i = 0; i < count; i++)
{
//Check if the entered title match with the
//any of the movies in the file or
not.
if (strcmp(movie[i].getTitle(), title) == 0)
{
//Call the printMovie function for the
//movie with the same title.
printMovie(movie[i]);
}
}
cout << endl;
}
//Define the function printRated to display the movies
//by their rating.
void Catalog::printRated()
{
Catalog c;
r = -1;
char rating[6] = "";
//Display the menu to choose a rating.
cout << "0 - NONE" << endl;
cout << "1 - G" << endl;
cout << "2 - PG" << endl;
cout << "3 - PG13" << endl;
cout << "4 - R" << endl;
cout << "5 - N17" << endl;
while (true)
{
//Call the function setrating to input the
//choice for the rating.
setrating(&c);
//Check if the choice entered by the user is
//valid or not.
if (c.r < 0 || c.r > 5)
{
cout << "Invalid choice. Please ";
cout << "re-enter: ";
}
else
break;
}
//Store the selected rating into the c-string
//rating.
if (c.r == 0)
strcpy_s(rating, "NONE");
else if (c.r == 1)
strcpy_s(rating, "G");
else if (c.r == 2)
strcpy_s(rating, "PG");
else if (c.r == 3)
strcpy_s(rating, "PG13");
else if (c.r == 4)
strcpy_s(rating, "R");
else if (c.r == 5)
strcpy_s(rating, "N17");
for (int i = 0; i < count; i++)
{
//Find the movie which has same rating as
//selected by the user.
if (strcmp(movie[i].getRating(), rating) == 0)
//Display the details of the movie with
//the same rating.
printMovie(movie[i]);
}
cout << endl;
}
//Define the function find to check if the entered
id //is valid or
not.
int Catalog::find(Movie movie[], int count, int id)
{
//Traverse the file.
for (int i = 0; i < count; i++)
{
//Check if the given id exists in the movie list
//or not.
if (movie[i].getId() == id)
return i;
}
return -1;
}
//Define the function addMovie to add a movie in the list.
void Catalog::addMovie()
{
//Prompt the user to enter a unique id.
Catalog c;
cout << "Movie ID? (must be unique) ";
//Call the function setid to input the id of the
//movie.
setid(&c);
//Check if the id is unique or not.
while (find(movie, count, c.id) != -1)
{
cout << "That ID is already in use. ";
cout << "Please enter another : ";
setid(&c);
}
//Set the id of the movie.
movie[count].setId(c.id);
//Prompt the user to input and set the title of the
//movie.
cin.ignore();
cout << "Title? ";
char title[250];
cin.getline(title, 250);
movie[count].setTitle(title);
//Prompt the user to input and set the year of the
//movie.
cout << "Year? ";
setyear(&c);
movie[count].setYear(c.year);
//Prompt the user to input and set the rating of the //movie.
cout << "Movie rating? " << endl;
r = -1;
cout << "0 - NONE" << endl;
cout << "1 - G" << endl;
cout << "2 - PG" << endl;
cout << "3 - PG13" << endl;
cout << "4 - R" << endl;
cout << "5 - N17" << endl;
while (true)
{
setrating(&c);
if (c.r < 0 || c.r > 5)
{
cout << "Invalid choice. Please ";
cout << "re-enter: ";
}
else
break;
}
if (c.r == 0)
strcpy_s(rating, "NONE");
else if (c.r == 1)
strcpy_s(rating, "G");
else if (c.r == 2)
strcpy_s(rating, "PG");
else if (c.r == 3)
strcpy_s(rating, "PG13");
else if (c.r == 4)
strcpy_s(rating, "R");
else if (c.r == 5)
strcpy_s(rating, "N17");
movie[count].setRating(rating);
//Prompt the user to input and set the copies of the
//movie.
cout << "Copies? ";
while (true)
{
setcopy(&c);
if (c.totalCopies <= 0)
{
cout << "Copies must not be negative. ";
cout << "Please re-enter: ";
}
else
{
movie[count].setCopies(c.totalCopies);
break;
}
}
//Prompt the user to input and set the rented copies //of the
movie.
cout << "rented? ";
while (true)
{
setrentcopy(&c);
if (c.rentedCopies <= 0)
{
cout << "Rented copies must not be ";
cout << "negative. Please re-enter:
";
}
else
{
movie[count].setRentedCopies(c.rentedCopies);
break;
}
}
//Increment the count variable.
count++;
}
//Define the function modifyMovie to modify a movie.
void Catalog::modifyMovie()
{
Catalog c;
char answer;
int index, choice = 0;
//Prompt the user to enter a choice y or n.
cout << "Do you want to see a list of movies ";
cout << "first? (y or n) ";
cin >> answer;
//If user enters y, then display the list of movies.
if (answer == 'y' || answer == 'Y')
printAll();
//Prompt the user to enter a id.
cout << "Enter id of movie: ";
while (true)
{
setid(&c);
//Check if the entered id exists or not.
index = find(movie, count, c.id);
if (index == -1)
{
cout << "The movie does not exist. ";
cout << "Please re-enter(or enter -1
";
cout << "to quit): " << endl;
}
else
{
//Display the menu to change the fields of
//the movie.
while (choice != 7)
{
cout << "--- Modify --- " <<
endl;
cout << "1 - id" << endl;
cout << "2 - title" <<
endl;
cout << "3 - year" <<
endl;
cout << "4 - rating" <<
endl;
cout << "5 - number of copies";
cout << endl;
cout << "6 - number of ";
cout << "rented copies" <<
endl;
cout << "7 - go back to main ";
cout << "menu" <<
endl;
cout << "Enter choice: ";
cin >> choice;
//Switch statements for different user
//choices.
switch (choice)
{
case 1:
{
//Prompt the user to input
//and set the unique
id
//of a movie for case 1.
cout << "Movie ID? (must
";
cout << "be unique) ";
setid(&c);
while (find(movie, count, c.id) !=
-1)
{
cout << "That ID is ";
cout <<
"already in ";
cout << "use. Please
";
cout << "enter ";
cout << "another:
";
setid(&c);
}
movie[index].setId(c.id);
break;
}
case 2:
{
//Prompt the user to input
//and set the title
//of a movie for case 2.
cout << "Title? ";
char title[250];
cin.ignore();
cin.getline(title, 250);
movie[index].setTitle(title);
break;
}
case 3:
{
//Prompt the user to input and set
//the year of a movie for case
3.
cout << "Year? ";
setyear(&c);
movie[index].setYear(c.year);
break;
}
case 4:
{
//Prompt the user to input
//and set the rating
//of a movie for case 4.
cout << "rating? " <<
endl;
r = -1;
cout << "0 - NONE" <<
endl;
cout << "1 - G" <<
endl;
cout << "2 - PG" <<
endl;
cout << "3 - PG13" <<
endl;
cout << "4 - R" <<
endl;
cout << "5 - N17" <<
endl;
while (true)
{
setrating(&c);
if (c.r < 0 || c.r >
5)
{
cout << "Invalid
";
cout << "choice.
";
cout << "Please
";
cout << "re-enter:
";
}
else
break;
}
if (c.r == 0)
strcpy_s(rating, "NONE");
else if (c.r == 1)
strcpy_s(rating, "G");
else if (c.r == 2)
strcpy_s(rating, "PG");
else if (c.r == 3)
strcpy_s(rating, "PG13");
else if (c.r == 4)
strcpy_s(rating, "R");
else if (c.r == 5)
strcpy_s(rating, "N17");
movie[index].setRating(rating);
break;
}
case 5:
{
//Prompt the user to
input //and
set the copies
//of a movie for case 5.
cout << "Copies? ";
setcopy(&c);
while (true)
{
if (c.totalCopies <=
0)
{
cout << "Copies
";
cout << "must
";
cout << "not be
";
cout << "negative.
";
cout << "Please re-
";
cout << "enter:
";
}
else
{
movie[index].setCopies(c.totalCopies);
break;
}
}
break;
}
case 6:
{
//Prompt the user to input
//and set the rented
copies
//of a movie for case 6.
cout << "rented? ";
setrentcopy(&c);
while (true)
{
if (c.rentedCopies <
0)
{
cout << "Rented
copies must not be negative. Please ";
cout << "re-enter:
";
}
else
{
movie[index].setRentedCopies(c.rentedCopies);
break;
}
}
break;
}
default:
return;
}
}
}
break;
}
}
//Define the function rentMovie to rent a movie.
void Catalog::rentMovie()
{
Catalog c;
char answer;
//Prompt the user to enter a choice y or n.
cout << "Do you want to see a list of movies ";
cout << "first? (y or n) ";
cin >> answer;
if (answer == 'y' || answer == 'Y')
//If user enters y, then display the list of
//movies.
printAll();
//Prompt the user to enter a valid id.
cout << "Enter id of movie: ";
while (true)
{
//Call this function defined in the main.cpp
//file to input the id.
setid(&c);
int index = find(movie, count, c.id);
if (index == -1)
{
cout << "The movie does not exist. Please
";
cout << "re-enter(or enter -1 to quit):
";
cout << endl;
}
else
{
//Check if the total copies and rented
//copies are equal or not.
if (movie[index].getRentedCopies() ==
movie[index].getCopies())
{
cout << "There are no copies to
rent.";
cout << endl;
}
else
{
//Decrement the value of the rented
//copies for the selected
movie.
int rented;
rented =
movie[index].getRentedCopies();
rented++;
movie[index].setRentedCopies(rented);
cout << "One copy of " <<
movie[index].getTitle() << " rented. " << endl;
}
break;
}
}
}
//Define the function returnMovie to return a movie.
void Catalog::returnMovie()
{
Catalog c;
char answer;
//Prompt the user to enter a choice y or n.
cout << "Do you want to see a list of movies ";
cout << "first? (y or n) ";
cin >> answer;
if (answer == 'y' || answer == 'Y')
//If user enters y, then display the list of
//movies.
printAll();
//Prompt the user to enter a valid id.
cout << "Enter id of movie: ";
while (true)
{
setid(&c);
int index = find(movie, count, c.id);
if (index == -1)
{
cout << "The movie does not exist. ";
cout << "Please re-enter(or enter -1 to
";
cout << "quit): " << endl;
}
else
{
//Check if the rented copies are equal to 0
//or not.
if (movie[index].getRentedCopies() == 0)
{
cout << "No copies were
rented.";
cout << endl;
}
else
{
//Increment the rented copies for
the
//selected movie.
int rented;
rented =
movie[index].getRentedCopies();
rented--;
movie[index].setRentedCopies(rented);
cout <<
movie[index].getTitle();
cout << " returned successfully.
";
cout << endl;
}
break;
}
}
}
//Define the function saveToFile to write the details of //all
the movies to the file catalog.txt.
void Catalog::saveToFile(char *filename)
{
//Open the file in the output mode.
ofstream outfile(filename, ios::out);
if (!outfile.is_open())
{
cout << "Error writing to output file ";
cout << filename << endl;
}
else
{
//Write the content of the movies to the file.
for (int i = 0; i < count; i++)
{
outfile << movie[i].getId() <<
endl;
outfile << movie[i].getTitle() <<
endl;
outfile << movie[i].getYear() <<
endl;
outfile << movie[i].getRating() <<
endl;
outfile << movie[i].getCopies() <<
endl;
outfile <<
movie[i].getRentedCopies();
outfile << endl;
}
//Close the file.
outfile.close();
}
}
//main.h
//Declare the required header files.
#include "stdafx.h"
#include <iostream>
#include "Catalog.h"
using namespace std;
//Declare the functions of the main.cpp file.
void setid(Catalog* c);
void setyear(Catalog* c);
void setcopy(Catalog* c);
void setrentcopy(Catalog* c);
void setrating(Catalog* c);
//main.cpp
//Include this header file while using visual studio.
#include "stdafx.h"
//Include the required header files.
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <ostream>
#include <cstring>
#include "main.h"
//Use the standard naming convention.
using namespace std;
//Define the function to input the id of a movie.
void setid(Catalog* c)
{
int id;
cin >> id;
//Set the id of the movie.
c->id = id;
}
//Define the function to input the year of a movie.
void setyear(Catalog* c)
{
int year;
cin >> year;
//Set the year of the movie.
c->year = year;
}
//Define the function to input the year of a movie.
void setcopy(Catalog* c)
{
int copy;
cin >> copy;
//Set the total copies of the movie.
c->totalCopies = copy;
}
//Define the function to input the rented copies of a
//movie.
void setrentcopy(Catalog* c)
{
int rented;
cin >> rented;
//Set the rented copies of the movie.
c->rentedCopies = rented;
}
//Define the function to input the choice for rating.
void setrating(Catalog* c)
{
int r;
cin >> r;
//Set the choice for the rating of the movie.
c->r = r;
}
//Start the execution of the main method.
int main()
{
//Create an object of the Catalog class.
Catalog c;
//Call the function readFile to read the text file.
c.readFile();
//Declare the required variables.
int choice = 0;
//Start the while loop.
while (choice!=8)
{
//Display the menu.
cout << " ********** MAIN MENU **********
";
cout << endl;
cout << "1 - Print Calalog" << endl;
cout << "2 - Search by Title" <<
endl;
cout << "3 - Search by Rating" <<
endl;
cout << "4 - Add Movie" << endl;
cout << "5 - Modify Movie" << endl;
cout << "6 - Rent Movie" << endl;
cout << "7 - Return Movie" << endl;
cout << "8 - Quit" << endl;
cout << "Enter choice: ";
cin >> choice;
//Switch statements for different user choices
switch (choice)
{
case 1:
//Call the function printAll to display the
//movies list.
c.printAll();
break;
case 2:
//Call the function printTitled to search
//the movie by their title.
c.printTitled();
break;
case 3:
//Call the function printRated to display
//the movies by their rating.
c.printRated();
break;
case 4:
//Call the function addMovie to add a movie
//to the file.
c.addMovie();
break;
case 5:
//Call the function modifyMovie to modify a
//movie.
c.modifyMovie();
break;
case 6:
//Call the function rentMovie to rent a
//movie.
c.rentMovie();
break;
case 7:
//Call the function returnMovie to return a
//movie.
c.returnMovie();
break;
case 8:
cout << "*** END OF PROGRAM *** " <<
endl;
break;
default:
//Display the default message.
cout << "Invalid Choice! Please enter a ";
cout << "valid
";
cout << "choice." << endl;
}
}
//Call the function saveToFile to write the details
//of the movie to the text file.
c.saveToFile("Catalog.txt");
//Use this system command while using visual studio.
system("pause");
//Return an integer value to the main function.
return 0;
}
moviestruct.cpp #include <iostream> #include <fstream> #include <cstdlib> #include <ostream> #include <fstream> #include <cstdlib> #include <cstring>...
fully comments for my program, thank you will thumb up #include <iostream> #include <fstream> #include <string> #include <iomanip> using namespace std; struct book { int ISBN; string Author; string Title; string publisher; int Quantity; double price; }; void choice1(book books[], int& size, int MAX_SIZE) { ifstream inFile; inFile.open("inventory.txt"); if (inFile.fail()) cout <<"file could not open"<<endl; string str; while(inFile && size < MAX_SIZE) { getline(inFile, str); books[size].ISBN = atoi(str.c_str()); getline(inFile, books[size].Title); getline(inFile, books[size].Author); getline(inFile, books[size].publisher); getline(inFile,...
I keep getting errors and i am so confused can someone please help and show the input and output ive tried so many times cant seem to get it. main.cpp #include <iostream> #include <vector> #include <string> #include "functions.h" int main() { char option; vector<movie> movies; while (true) { printMenu(); cin >> option; cin.ignore(); switch (option) { case 'A': { string nm; int year; string genre; cout << "Movie Name: "; getline(cin, nm); cout << "Year: "; cin >> year; cout << "Genre: "; cin >> genre; //call you addMovie() here addMovie(nm, year, genre, &movies); cout << "Added " << nm << " to the catalog" << endl; break; } case 'R': { string mn; cout << "Movie Name:"; getline(cin, mn); bool found; //call you removeMovie() here found = removeMovie(mn, &movies); if (found == false) cout << "Cannot find " << mn << endl; else cout << "Removed " << mn << " from catalog" << endl; break; } case 'O': { string mn; cout << "Movie Name: "; getline(cin, mn); cout << endl; //call you movieInfo function here movieInfo(mn, movies); break; } case 'C': { cout << "There are " << movies.size() << " movies in the catalog" << endl; // Call the printCatalog function here printCatalog(movies); break; } case 'F': { string inputFile; bool isOpen; cin >> inputFile; cout << "Reading catalog info from " << inputFile << endl; //call you readFromFile() in here isOpen = readFile(inputFile, &movies); if (isOpen == false) cout << "File not found" << endl;...
#include <fstream> #include <iostream> #include <cstdlib> using namespace std; // Place charcnt prototype (declaration) here int charcnt(string filename, char ch); int main() { string filename; char ch; int chant = 0; cout << "Enter the name of the input file: "; cin >> filename; cout << endl; cout << "Enter a character: "; cin.ignore(); // ignores newline left in stream after previous input statement cin.get(ch); cout << endl; chcnt = charcnt(filename, ch); cout << "# of " «< ch« "'S:...
in c++ please program for this code #include <iostream> #include <fstream> #include <string> #include <cstring> // for string tokenizer and c-style string processing #include <algorithm> // max function #include <stdlib.h> #include <time.h> using namespace std; // Extend the code here as needed class BTNode{ private: int nodeid; int data; int levelNum; BTNode* leftChildPtr; BTNode* rightChildPtr; public: BTNode(){} void setNodeId(int id){ nodeid = id; } int getNodeId(){ return nodeid; } void setData(int d){ data = d; } int getData(){ return data;...
WONT COMPILE ERROR STRAY / TEXT.H AND CPP PROGRAM SAYING NOT DECLARED HAVE A DRIVER WILL PASTE AT BOTTOM MOVIES.CPP #include "movies.h" #include "Movie.h" Movies *Movies::createMovies(int max) { //dynamically create a new Movies structure Movies *myMovies = new Movies; myMovies->maxMovies = max; myMovies->numMovies = 0; //dynamically create the array that will hold the movies myMovies->moviesArray = new Movie *[max]; return myMovies; } void Movies::resizeMovieArray() { int max = maxMovies * 2; //increase size by 2 //make an array that is...
Can you tell me what is wrong and fix this code. Thanks #include <iostream> #include <string> #include <fstream> #include <sstream> using namespace std; //function void displaymenu1(); int main ( int argc, char** argv ) { string filename; string character; string enter; int menu1=4; char repeat; // = 'Y' / 'N'; string fname; string fName; string lname; string Lname; string number; string Number; string ch; string Displayall; string line; string search; string found; string document[1000][6]; ifstream infile; char s[1000];...
-can you change the program that I attached to make 3 file songmain.cpp , song.cpp , and song.h -I attached my program and the example out put. -Must use Cstring not string -Use strcpy - use strcpy when you use Cstring: instead of this->name=name .... use strcpy ( this->name, name) - the readdata, printalltasks, printtasksindaterange, complitetasks, addtasks must be in the Taskmain.cpp - I also attached some requirements below as a picture #include <iostream> #include <iomanip> #include <cstring> #include <fstream>...
#include <iostream> #include <string> #include <fstream> #include <sstream> using namespace std; struct transition{ // transition structure char start_state, to_state; char symbol_read; }; void read_DFA(struct transition *t, char *f, int &final_states, int &transitions){ int i, j, count = 0; ifstream dfa_file; string line; stringstream ss; dfa_file.open("dfa.txt"); getline(dfa_file, line); // reading final states for(i = 0; i < line.length(); i++){ if(line[i] >= '0' && line[i] <= '9') f[count++] = line[i]; } final_states = count; // total number of final states // reading...
The following is a sample inventory in C++, i want to ask the user to input a item number for removing from inventory. //CPP #include <iostream> #include <fstream> #include <cstdlib> #include <iomanip> #define MAX 1000 using namespace std; //Function to Add a new inventory item to the data into the array in memory void addItem(string desc[],string idNum[], float prices[], int qty[],int &num) { cout<<"Enter the names:"; cin>>desc[num]; cout<<"Enter the item number:"; cin>>idNum[num]; cout<<"Enter the price of item:"; cin>>prices[num]; cout<<"Enter the...
graph binary search for size and time c++ //System Libraries #include <iostream> #include <string> #include <cstdlib> #include <ctime> #include <iomanip> #include <algorithm> using namespace std; //User Libraries //Global Constants, no Global Variables are allowed //Math/Physics/Conversions/Higher Dimensions - i.e. PI, e, etc... //Function Prototypes //Execution Begins Here! int main(int argc, char** argv) { int n, i, arr[50], search, first, last, middle,count=0,count_in,tot; clock_t start, end; float duration; cout<<"Enter total number of elements :"; cin>>n; cout<<"Enter numbers"; for (i=0; i<n;i++) cin>>arr[i]; cout<<"Enter a...