The code and details below. Looks like just another finish my homework for me question:
I need to do the following steps to the program:
Add a destructor and delete/delete[ ] as appropriate to eliminate any memory leaks.
Can change all code back to using a string rather than a c-string.
Also set pointers to null as/if needed.
Replace the formatted output method (toString) with an overloaded output/extraction operator, and modify the driver to use the overloaded operator.
Creating a copy constructor for a deep copy is probably not warranted as the title is the only object data member, but use references/pointers as needed in order to suppress the automatic invocation of the copy constructor
Here is the code:
Code:
// Movie.cpp
#include "Movie.h" // include Movie class definition
#include <sstream>
#include <string>
using namespace std;
Movie::Movie() {
title = new char[1];
*title = '\0';
usBoxOffice = nonUSBoxOffice = worldBoxOffice = usRank = nonUSRank =
worldRank = 0;
}
Movie::Movie(const char * temp) {
istringstream iS(temp);
string s;
getline(iS, s, '\t');
title = new char[s.length() + 1];
strcpy(title, s.c_str());
iS >> usRank >> nonUSRank >> worldRank >> usBoxOffice
>> nonUSBoxOffice >> worldBoxOffice;
}
const char * Movie::getTitle() const {return title;}
long long Movie::getUSBoxOffice() const {return usBoxOffice;}
long long Movie::getNonUSBoxOffice() const {return nonUSBoxOffice;}
long long Movie::getWorldBoxOffice() const {return worldBoxOffice;}
int Movie::getUSRank() const {return usRank;}
int Movie::getNonUSRank() const {return nonUSRank;}
int Movie::getWorldRank() const {return worldRank;}
const char * Movie::toString() const {
ostringstream oS;
oS << "\n\n====================== Movie Information\n"
<< "\n Movie Title:\t" << title
<< "\n US Rank & Box Office:\t" << usRank << "\t$" << usBoxOffice
<< "\nNon-US Rank & Box Office:\t" << nonUSRank << "\t$" << nonUSBoxOffice
<< "\n World Rank & Box Office:\t" << worldRank << "\t$" << worldBoxOffice
<< "\n";
char * s = new char[500];
strcpy(s, oS.str().c_str());
return s;
}
// Movie.h
#ifndef MOVIE_H
#define MOVIE_H
using namespace std;
class Movie {
// data is private by default
char *title;
long long usBoxOffice, nonUSBoxOffice, worldBoxOffice;
short usRank, nonUSRank, worldRank;
public:
Movie()
{ title = new char;
}
~ movie()
{
delete title;
}
Movie * returnnew ()
{
a retval;
returnt (&retval);}
Movie(const char *);
const char * getTitle() const;
long long getUSBoxOffice() const;
long long getNonUSBoxOffice() const;
long long getWorldBoxOffice() const;
int getUSRank() const;
int getNonUSRank() const;
int getWorldRank() const;
const char * toString() const;
};
#endif
// MovieInfoApp.cpp
#include "Movie.h" // include Movie class definition
#include "Movies.h" // include Movies class definition
#include <iostream>
#include <iomanip>
using namespace std;
void main() {
Movies movies("imdb 01-16-2011.txt");
Movie * foo = returnnew ();
if(movies.getMovieCount() > 0) {
char movieCode[81];
cout << "Please enter the movie search string or #number (press Enter to exit): ";
cin.getline(movieCode, 80);
if (strlen(movieCode) > 0) {
do {
const Movie * m;
if(movieCode[0]=='#')
m = movies.getMovie(atoi(movieCode+1));
else
m = movies.getMovie(movieCode);
if(m != NULL) {
cout << m->toString() << "\n";
if(m->getWorldBoxOffice() > 0)
cout << setprecision(2) << fixed
<< "\n\tUS to World Ratio: "
<< (m->getUSBoxOffice() * 100.0) /
m->getWorldBoxOffice() << "%\n" << endl;
} else
cout << "\n Movie not found!\n\n" << endl;
cout << "Please enter the movie search string or #number (press Enter to exit): ";
cin.getline(movieCode, 80);
} while (strlen(movieCode) > 0);
}
}
}
// Movies.cpp
#include "Movie.h" // include Movie class definition
#include "Movies.h" // include Movies class definition
#include <fstream>
using namespace std;
Movies::Movies(const char * fn){loadMovies(fn);}
int Movies::getMovieCount() const {return movieCnt;}
const Movie * Movies::getMovie(const char * mc) const {
if(strlen(mc)==0)
return NULL; // not found
else {
char *mcP = myToLower(mc);
int ndx=0;
for(;ndx<movieCnt &&
(strstr(myToLower(movies[ndx].getTitle()), mcP)==
NULL);ndx++);
return ndx<movieCnt?&movies[ndx]:NULL;
}
}
const Movie * Movies::getMovie(int mc) const {
if(mc <= 0 || mc > movieCnt)
return NULL; // not found
return &movies[mc-1];
}
void Movies::loadMovies(const char * fn) {
ifstream iS(fn);
const int LINE_LEN = 300;
char s[LINE_LEN+1];
iS.getline(s, LINE_LEN); // skip heading
iS.getline(s, LINE_LEN);
movieCnt=0;
movies = new Movie[MAX_MOVIES];
while(!iS.eof()) {
movies[movieCnt++] = Movie(s);
iS.getline(s, LINE_LEN);
}
iS.close();
reSize();
}
void Movies::reSize() {
Movie * m = movies;
movies = new Movie[movieCnt];
for(int i=0;i<movieCnt;i++)
movies[i] = m[i];
}
char * Movies::myToLower(const char * s) const {
int n = strlen(s);
char * t = new char[n+1];
for(int i=0;i<n;i++)
t[i] = tolower(s[i]);
t[n] = '\0';
return t;
}
// Movies.h
#ifndef MOVIES_H
#define MOVIES_H
#include "Movie.h" // include Movie class definition
using namespace std;
class Movies {
// data is private by default
static const int MAX_MOVIES = 1000;
Movie *movies;
Movie = newMovie [size];
delete [] movies;
int movieCnt;
public:
Movies(const char *);
int getMovieCount() const;
const Movie * getMovie(const char *) const;
const Movie * getMovie(int) const;
private:
void loadMovies(const char *);
char * myToLower(const char *) const;
void reSize();
};
#endif