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; }



LinkBack URL
About LinkBacks



