simple crash problem (exiting func)

This is a discussion on simple crash problem (exiting func) within the C++ Programming forums, part of the General Programming Boards category; Ive set up a simple program that is going to (eventually) store information about dvd's etc and write them all ...

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    34

    simple crash problem (exiting func)

    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;
    }
    Last edited by te5la; 09-16-2008 at 02:09 PM.

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    You never did a dvds.push_back() or anything. By the way, you may wish to open your database with the append flags too.

  3. #3
    Registered User
    Join Date
    Jul 2008
    Posts
    34
    Quote Originally Posted by master5001 View Post
    You never did a dvds.push_back() or anything. By the way, you may wish to open your database with the append flags too.
    I do? as it adds it to the list the first time round as well as the second but it is when it exits this function for the second time that the problem occurs.

    Code:
    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());  <<<<<< PUSH_BACK HERE
    				correct = false;
    			}
    			else
    			{
    				throw string("Invalid Item");
    			}
    		}
    		catch(string message)
    		{
    			system("cls");
    			cout << message << "Entered..." << endl;
    		}
    	}
    }
    Not sure what you mean by the append flag.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple File I/O problem
    By Ignited in forum C++ Programming
    Replies: 3
    Last Post: 01-07-2006, 09:49 AM
  2. Fairly simple problem
    By fatdunky in forum C Programming
    Replies: 1
    Last Post: 11-14-2005, 10:34 PM
  3. Im a Newbie with a graphics design problem for my simple game
    By Robert_Ingleby in forum C++ Programming
    Replies: 1
    Last Post: 11-23-2001, 05:41 PM
  4. Simple boolean problem
    By larry in forum C++ Programming
    Replies: 9
    Last Post: 10-13-2001, 08:49 AM
  5. A Simple (?) Problem
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 10-12-2001, 04:28 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21