Can't wrap my brain around this:

This is a discussion on Can't wrap my brain around this: within the C++ Programming forums, part of the General Programming Boards category; Ok back to my CD Database: I'm still trying to add the track list but I decided to implement it ...

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    16

    Can't wrap my brain around this:

    Ok back to my CD Database:

    I'm still trying to add the track list but I decided to implement it with another class mainly because I couldn't figure it out any other way.

    So I have a Database Class, with a databaseArray which contains pointers to a CD, another class that I have which contains the artist, title, and year. But in order to get a track list on there, I created a Track class, which lists CD class as it's friend and simply just holds as a private member char* track. Therefore, the CD class holds a trackArray. In my mind this should work out correctly, but for some reason it is not.

    So, before I created the track list, I would read in from a file and save each element as a temp string, and then call the add function which takes a pointer to a CD as follows:

    Code:
    Database cdDatabase;
    char *tempArtist[30];
    char *tempTitle[30];
    char *tempYear[30];
    
    cdDatabase.addCD(new CD(tempArtist,tempTitle,tempYear));

    This all works fine, but when I read in the track list in my reading in loop I get a little confused. What I did was created a temp CD object, and then add the tracks to a temp CD as such:

    Code:
    tempCD.addTrack(new Track(tempTrack));

    Now my problem is how to add this to the database. How can I use the first line of code that I stated above add the track list as well. Like I want to add in tempArtist,tempTitle,tempYear, and then the track list but I can't access it from main.

    Am I going about this all wrong?

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    16
    Is everyone else that viewed this stuck too?

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Don't bump your threads. Go back and read the forum Announcements again.

    You do realize that all of those are just pointers, which don't have any memory allocated for them, right? Also, why aren't you just using a structure for all of this? Or a class? Wrap all of those individual items into a single record, and have functions to populate them all.

    Or don't.

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Here's a basic schematic to write user input to file based on your previous post. The example hasn't been compiled so I can't vouch for completeness.
    Code:
    char artist[30];
    char  track[30];
    char header[] = "---CD---\n";
    char footer[] = "------\n";
    bool anotherArtist = true;
    bool moreTracks = true;
    char ch;
    ofstream fout("myCDfile.txt");
     
    while(anotherArtist)
    {
       fout << header;
       cout << "enter artist" << endl;
       cin >> artist;
       fout << artist << '\n';
       while(moreTracks)
       {
    	  cout << "enter track" << endl;
    	  cin >> track;
    	  fout << track << '\n';
    	  cout << "enter n to stop" << endl;
    	  cin >> ch;
    	  if(ch == 'n')
    	  {
    		 moreTracks = false;
    		 fout << footer;
    	  }
       }
       cout << "enter y for another Artist" << endl;
       cin >> ch;
       if(ch != 'y')
    	 fout << '\n';
    }
    To use OOP you could try this as a start (again code not tested):
    Code:
    struct CD
    {
       string artist;
       vector<string> tracks;
       void intializeCD();
       friend void operator<<(ostream & fout, CD & cd);
    };
     
    void CD::initializeCD()
    {
       string track;
       char ch;
       bool moreTracks = true;
     
       cout << "enter artist" << endl;
       cin >> artist;
       
       while(moreTracks)
       {
    	  cout << "enter track" << endl;
    	  cin >> track;
    	  tracks.push_back(track);
     
    	  cout << "enter another track   y/n" << endl;
    	  cin >> ch;
    	  if(ch == 'n')
    		moreTracks = false;
       }
    }
    	  
    void operator<<(ostream & fout, CD & cd)
    {
      fout << "--CD--" << '\n';
      fout << cd.artist << '\n';
      int i = 0;
      vector<string>::iterator it = tracks.begin();
      for( ; it != tracks.end(); ++it)
    	 fout << *it << '\n';
      fout << "------" << '\n'
    }
    You're only born perfect.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Human brain and programing
    By avgprogamerjoe in forum A Brief History of Cprogramming.com
    Replies: 26
    Last Post: 08-27-2007, 04:48 PM
  2. Word Wrap
    By sethjackson in forum Windows Programming
    Replies: 4
    Last Post: 09-21-2005, 04:35 PM
  3. Mouse to have human brain
    By nickname_changed in forum A Brief History of Cprogramming.com
    Replies: 22
    Last Post: 03-10-2005, 04:39 PM
  4. brain vs. computer (brain wins hands down)
    By hk_mp5kpdw in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 09-17-2003, 08:41 PM
  5. Re: Girlfriend Post
    By PsychoBrat in forum A Brief History of Cprogramming.com
    Replies: 57
    Last Post: 05-13-2002, 06:11 AM

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