#include <iostream>
    #include <cstring>

    using namespace std;

    class CD
    {

    public : 
	    void getCDdetails(CD *);
	    void printCDD(CD *) const;
	    int putCount() const
	    {
	    return count;
	    }

    private :
	static int count;
	char Title[60];
	char Artist[60];
	char Type;
	int Tracks;
    };


    int CD :: count=0;

    void CD :: getCDdetails(CD * cd)
    {
    cd=new CD[putCount() + 1];  //THIS LINE IS NOT HELPING
	    for(int j=putCount(); j < 100; j++)
	    {
	    cout << "\nTitle :";
	    cin >> cd[j].Title;
	    cout << "\nArtist :";
	    cin >> cd[j].Artist;
	    cout << "\nType :";
	    cin >> cd[j].Type;
	    cout << "\nTracks";
	    cin >> cd[j].Tracks;
	    count++;
	    cout << "Details added, Press enter to continue";
	    fflush(stdin);
	    getchar();
	    break;;
	    }
	    printCDD(cd);
    }


    void CD :: printCDD(CD * cd) const 
    {

    cout << "\nThere are " << putCount() << " number of CD details in the database at  present";
	    for(int j=0; j < putCount(); j++)
	    {
	    cout << "\nTitle : " << cd[j].Title;
	    cout << "\nArtist : " << cd[j].Artist;
	    cout << "\nType : " << cd[j].Type;
	    cout << "\nTracks " << cd[j].Tracks << endl;
	    }
	    fflush(stdin);
	    cout << "\nPress enter to continue";
	    getchar();
    }


    void display_menu();

    int main()
    {
    display_menu();
    return 0;
    }

    void display_menu()
    {
    CD * st;
    int choice;
    int j;
    for(;;)
    {
    system("clear");
    cout << "\n\t\t\t\tCurrent number of CDs in the CD database is  " << st[0].putCount();
    cout << "\n1:Enter a CD detail"   \
         << "\n2:Print all the CD details" \
         << "\n3:Quit"  \
         << "\nEnter your choice : ";
    cin >> choice;
         switch(choice)
         {
         case 1 :
        	st[0].getCDdetails(st);
     	        break;
         case 2 :
     	        st[0].printCDD(st);
     	        break;
         case 3 :
               exit(0);
         default :
     	        cout << "\nPlease enter valid choice";
     	        fflush(stdin);
     	        getchar();
     	        break;
         }
      }    	    
    }

This line(cd=new CD[putCount() + 1];) does not helps because everytime I go for adding a data to the object array then perhaps a new chunk of memory gets allocated and the data added previously gets lost

example : when i add to the ist object its O.K

Current number of CDs in the CD database is 0
1:Enter a CD detail
2:Print all the CD details
3:Quit
Enter your choice : 1
Title :a
Artist :a
Type :a
Tracks1
Details added, Press enter to continue

There are 1 number of CD details in the database at present
Title : a
Artist : a
Type : a
Tracks 1

Press enter to continue

1:Enter a CD detail
2:Print all the CD details
3:Quit
Enter your choice : 1
Title :a
Artist :a
Type :a
Tracks1
Details added, Press enter to continue

There are 1 number of CD details in the database at present
Title : a
Artist : a
Type : a
Tracks 1

Press enter to continue

Current number of CDs in the CD database is 1
1:Enter a CD detail
2:Print all the CD details
3:Quit
Enter your choice : 1
Title :b
Artist :b
Type :b
Tracks2
Details added, Press enter to continue

There are 2 number of CD details in the database at present
**Title :
Artist :
Type :
Tracks 0** //ITS NOT DISPLAYING THAT WAS ADDED JUST BEFORE

Title : b
Artist : b
Type : b
Tracks 2

Press enter to continue

If its painful to look at it can somebody show me some example how to dynamically add to an object array ( I am just trying to do with new operator first ) and I have not tried linked list if I am successful I will go ahead and use linked list also.

Recommended Answers

All 3 Replies

Regardless of anything else, the whole thing breaks the first time you hit line 84 as far as I can tell.

What's st[0]? Is st an array? If so, you never set aside any memory for it, or at least not soon enough.

>> This line(cd=new CD[putCount() + 1])...

Does it ever even get this far? I guess it does from your printout.

OK, I guess I see why it "worked" now. This is some strange looking code.

Line 45 - If you have this break statement, why bother having a loop?

If you are going to have an array, allocate storage for it on line 78, not line 30.

Line 31 - Where'd the 100 come from? What's that?

I don't think you should have a variable called count in your CD class. Your class is called "CD", as in a single CD. Handle CD COLLECTIONS elsewhere.

Thanks, I will refer to your advice

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.