Simple issue please help

This is a discussion on Simple issue please help 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 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;
    }

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    First off, deja vu... Didn't I answer this question before...

    Quote Originally Posted by te5la View Post
    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;
    }

  4. #4
    Registered User
    Join Date
    Jul 2008
    Posts
    34
    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?

  5. #5
    Registered User
    Join Date
    Jul 2008
    Posts
    34
    sorted it thanks, forgot to send a pointer to function.

  6. #6
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with probably a very simple char sting issue
    By Prometheus in forum C++ Programming
    Replies: 14
    Last Post: 01-10-2007, 07:02 PM
  2. Simple issue but I am STUCK
    By jedispy in forum C++ Programming
    Replies: 2
    Last Post: 12-01-2006, 01:02 AM
  3. ...deceptively simple...
    By Sebastiani in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 07-29-2002, 12:51 PM
  4. Need help with simple DAQ program
    By canada-paul in forum C++ Programming
    Replies: 12
    Last Post: 03-15-2002, 07:52 AM
  5. How can I issue AT commands in VC++ ?
    By Cube in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 10-10-2001, 07:45 AM

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