-
Simple issue please help
Ive set up a simple program that is going to (eventually) store information about dvd's etc and write them all to file. The problem is that it crashed when i try to enter a 2nd dvd to the list (specifically when exiting the addToDatabase function). Any help would be much appreciated.
This code is ok to copy and paste into a compiler and ran. Simply run and enter 2 when prompted for an option. Here enter 'dvd' and then enter a movie name. This will add it to a list and then take you back to the begining, now try and follow the same procedure and it will crash.
Code:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
class digitalMedia
{
public:
digitalMedia()
{
name = "";
format = "";
}
void setName(string x){ name = x; }
void setFormat(string x){ format = x; }
string getName(){ return name; }
string getFormat(){ return format; }
void virtual writeToFile(ofstream &stream);
protected:
string name;
string format;
};
class dvd: public digitalMedia
{
public:
dvd()
{
director = "";
}
void setDirector(string x){ director = x;}
string getDirector(){ return director;}
protected:
string director;
};
dvd addDvd()
{
dvd newDvd;
string enteredStr;
system("cls");
cout << "Please Enter The Title: ";
cin >> enteredStr;
newDvd.setFormat("DVD");
newDvd.setName(enteredStr);
return newDvd;
}
void addToDatabase(ofstream data,vector<dvd> &dvds)
{
string item;
while(bool correct)
{
try
{
cout << "Please Enter Format (either dvd, game or book): ";
cin >> item;
if(item == "dvd")
{
cout << "dvd entered";
dvds.push_back(addDvd());
correct = false;
}
else
{
throw string("Invalid Item");
}
}
catch(string message)
{
system("cls");
cout << message << "Entered..." << endl;
}
}
cout << endl << endl << dvds[0].getName() << endl;
cout << "exited addtodatabase";
}
void mainMenu(int inputVal, ofstream &database,vector<dvd> &dvds)
{
system("cls");
cout << " DVD, GAME & BOOK RENTAL SYSTEM " << endl;
cout << "1: View Current Database " << endl << endl;
cout << "2: Add To Database " << endl << endl;
cout << "3: Rent An Item " << endl << endl;
cout << endl << endl << "Please Choose An Option: ";
cin >> inputVal;
switch(inputVal)
{
case 1:
break;
case 2:
system("cls");
addToDatabase(database, dvds);
cout << "in switch";
break;
default:
cout << "Invalid Selection, Please Re-Type.";
break;
}
}
int main()
{
vector<dvd> dvds;
ofstream database("database.txt");
int inputVal = 0;
mainMenu(inputVal, database, dvds);
mainMenu(inputVal, database, dvds);
cout << "about to begin getname on list";
cout << endl << endl << dvds[0].getName() << endl;
database.close();
cout << endl << endl << endl;
system("PAUSE");
return 0;
}
-
don't you have any compilation warnigns?
Code:
mainMenu(int inputVal
why do you need inputVal as a parameter and not as a local var?
Code:
addToDatabase(ofstream data
are you sure you could pass stream not by ref?
-
First off, deja vu... Didn't I answer this question before...
Quote:
Originally Posted by
te5la
Code:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
class digitalMedia
{
public:
digitalMedia() : name(""), format("") // Nothing huge here... Just my preference
{
}
void setName(string x){ name = x; }
void setFormat(string x){ format = x; }
string getName() const{ return name; }
string getFormat() const{ return format; }
void virtual writeToFile(ofstream &stream);
protected:
string name;
string format;
};
class dvd: public digitalMedia
{
public:
dvd() : director("") // again... my preference
{
}
void setDirector(string x){ director = x;}
string getDirector const(){ return director;}
protected:
string director;
};
dvd addDvd()
{
dvd newDvd;
string enteredStr;
system("cls");
cout << "Please Enter The Title: ";
cin >> enteredStr;
newDvd.setFormat("DVD");
newDvd.setName(enteredStr);
return newDvd;
}
void addToDatabase(ofstream data,vector<dvd> &dvds)
{
string item;
while(bool correct)
{
try
{
cout << "Please Enter Format (either dvd, game or book): ";
cin >> item;
if(item == "dvd")
{
cout << "dvd entered";
dvds.push_back(addDvd());
correct = false;
}
else
{
throw string("Invalid Item");
}
}
catch(string message)
{
system("cls");
cout << message << "Entered..." << endl;
}
}
cout << endl << endl << dvds[0].getName() << endl;
cout << "exited addtodatabase";
}
void mainMenu(int inputVal, ofstream &database,vector<dvd> &dvds)
{
system("cls");
cout << " DVD, GAME & BOOK RENTAL SYSTEM " << endl;
cout << "1: View Current Database " << endl << endl;
cout << "2: Add To Database " << endl << endl;
cout << "3: Rent An Item " << endl << endl;
cout << endl << endl << "Please Choose An Option: ";
cin >> inputVal;
switch(inputVal)
{
case 1:
break;
case 2:
system("cls");
addToDatabase(database, dvds);
cout << "in switch";
break;
default:
cout << "Invalid Selection, Please Re-Type.";
break;
}
}
int main()
{
vector<dvd> dvds;
ofstream database("database.txt"); // You may want to open this as read/write access with the append flag, right?
int inputVal = 0;
mainMenu(inputVal, database, dvds);
mainMenu(inputVal, database, dvds);
cout << "about to begin getname on list";
cout << endl << endl << dvds[0].getName() << endl;
database.close();
cout << endl << endl << endl;
system("PAUSE");
return 0;
}
-
thanks for the replies but neither addresses the problem. It still crashes when trying to add a second dvd to the list. No compilation warning is given only when ran a windows warning screen appears when adding a second dvd.
Any ideas?
-
sorted it thanks, forgot to send a pointer to function.
-
I am glad you got it sorted out, but some of the items I hightlighted are still worth examining. The const thing is actually very important to get in the habit of using correctly. I actually would change those to return a const string& instead, to be honest. The references to dvd[0] are extremely presumptuous. You are just giving far too much credit to the user's ability to play by the rules. Sometimes we all accidentally hit the wrong key. Which could result in a list being generated that contains no dvds. In which case dvd[0] should contain junk if it doesn't buffer overflow.