I'm trying to help my friend with an assignment that he has absolutely no clue on. He confided in me that programming isn't what he wants to do, and he's switching his major next year. He wants to keep up good grades so he can switch to engineering easily, normally I wouldn't practically write someone's assignment for them, but I understand his situation so I'm trying to help him out (btw he doesn't go to my school).
He's in Intro to OOP using C++, and his assignment is to develop a Video Rental System for a shop that rents out DVD's and VCD's (Video CD's) to it's customers. The system must keep track of its rental information and compute rental fees from the DVD's and VCD's rented out to customers.
Objects are required for this assignment. At minimum, a class for a Customer, and a class for a Movie. My friend implemented these. I also implemented a VideoStore class to manage objects of type Customer and Movie. I'm trying to keep it as simple as possible for him, after all, he has to explain to his teacher what he did. I'm trying to set up a skeleton and a few methods so he can do the rest. The original code wasn't very pretty. The leak seems to be in the class I wrote (VideoStore) in method addMovie(). Other methods in the class definition haven't been written yet.
The program compiles and executes, but look at function main()
Code:
#include "videostore.h"
int main()
{
VideoStore v1;
v1.addMovie();
v1.addMovie();
v1.printMovies();
return 0;
}
addMovie prompts for 2 char array strings, then is supposed to copy them to their respective object (Movie). The program crashes and exits after the second string is entered, and v1.addMovie(); never executes a second time.
Here's the code:
Code:
customer.h
#ifndef CUSTOMER_H
#define CUSTOMER_H
class Customer {
char Name[40];
int Age;
char CustCode[10];
public:
static int cCount;
Customer()
{
Name[0] = '\0';
CustCode[0] = '\0';
Age = 0;
}
Customer(char *n, char *c, int a)
{
strcpy(Name,n);
strcpy(CustCode, c);
Age = a;
}
void setCustomer(char *n, char *c, int a)
{
strcpy(Name,n);
strcpy(CustCode, c);
Age = a;
}
void display()
{
cout << Name << " " << Age << " " << CustCode << endl;
}
char *theName()
{
return Name;
}
int theAge()
{
return Age;
}
};
#endif
Code:
movie.h
#ifndef MOVIE_H
#define MOVIE_H
class Movie
{
private:
char title[40];
char desc[5];
int serialnum;
public:
static int mCount;
Movie()
{
title[0] = '\0';
desc[0] = '\0';
serialnum = 0;
}
Movie(char *t,char *d)
{
strcpy(title, t);
strcpy(desc, d);
}
char* getTitle()
{
return title;
}
void setMovie(char* t, char* d)
{
strcpy(title, t);
strcpy(desc, d);
}
void displayMovie()
{
cout << title << " " << desc << endl;
}
};
#endif
Code:
videostore.h
#ifndef VIDEOSTORE_H
#define VIDEOSTORE_H
#include "movie.h"
#include "customer.h"
class VideoStore
{
private:
Customer* customerArrayPtr;
Movie* movieArrayPtr;
unsigned numMovies;
unsigned numCustomers;
public:
VideoStore();
~VideoStore();
void addMovie();
void modifyMovie();
void printMovies();
void addCustomer();
void modifyCustomer();
void deleteCustomer();
};
VideoStore::VideoStore()
{
customerArrayPtr = 0;
movieArrayPtr = 0;
numMovies = 0;
numCustomers = 0;
}
VideoStore::~VideoStore()
{
delete [] customerArrayPtr;
delete [] movieArrayPtr;
}
void VideoStore::addMovie()
{
numMovies++;
Movie * tmpPtr = new Movie[ numMovies];
char tTitle[40];
char tDesc[5];
// copy array, element by element
if(numMovies != 1)
{
for(int i = 0; i <= numMovies; i++)
tmpPtr[i] = movieArrayPtr[i];
delete [] movieArrayPtr;
// just have movieArrayPtr point to the new array
movieArrayPtr = tmpPtr;
delete [] tmpPtr;
}
// are you gonna actually add a movie in here somewhere
// like the function name suggests? You probably want to take
// a Movie object as a paramter. better yet a reference to a Movie
// object, make sure your Movie class has a copy constructor
// and operator= function
// do input/output to read movie into VideoStore
cout << "Enter Movie Name: ";
cin.getline(tTitle, 40);
cout << "Enter DVD or VCD: ";
cin.getline(tDesc, 5);
movieArrayPtr[numMovies].setMovie(tTitle, tDesc);
}
void VideoStore::modifyMovie()
{
char tTitle[40];
char tDesc[5];
int i;
bool isFound = false;
cout << endl;
cout << "Enter Movie Name Which You Wish To Modify : ";
cin.getline(tTitle,40);
for(i = 0; i < numMovies; i++)
{
if ( stricmp(tTitle, movieArrayPtr[i].getTitle()) == 0 )
{
cout << "\nRe-Enter Movie Name : ";
cin.getline(tTitle, 40);
cout << "\nRe-Enter Medium Type, e.g VCD, DVD : ";
cin.getline(tDesc, 5);
movieArrayPtr[i].setMovie(tTitle, tDesc);
break;
}
}
if(!isFound)
cout << "Name Not In The List!" << endl;
}
void VideoStore::printMovies()
{
for(int i = 0; i < numMovies; i++)
movieArrayPtr[i].displayMovie();
}
#endif
And here's main
Code:
main.cpp
#include <iostream>
using namespace std;
//#include "customer.h"
//#include "movie.h"
#include "videostore.h"
int main()
{
VideoStore v1;
v1.addMovie();
v1.addMovie();
v1.printMovies();
return 0;
}
Help would be very appreciated.