Hi folks i've been working hard with an assignment for college but have become stuck. Hoping u kind guys could give me some pointers. The assignment section reads:

MP3 Player
As stated above, this section of the player will not be loading and actually playing a
genuine MP3 file, it will handle a simple simulation.
Contain a minimum of five —“MP3 files” though be able to contain an unlimited number.
Select next M P3.
Select previous M P3.
Show name, current position and length of M P3.
e.g. Bob Dylan Hurricane 12/52
Step forward by one position unit. Moving to next if at end.
Step forward by ten position units. Not passing end.
Step backwards by ten position units.
Not passing start.
When select next or previous M P3 the system should automatically wrap around both ways so that it will step from the fifth to the first when selecting —next“ or — step forward“ or from the fifth to the first when selecting —previous“.

You should choose your own titles for the MP3 files.
The details of each —MP3 file should be contained within and loaded from a suitable text file that is accessed by you application and readable using a suitable text editor such as Notepad™ .
Loading is only permitted when the overall system starts, not when switching to this player.
The system should automatically save the loaded / stored M P3 files at termination overall application not when switching away from this player.

So to sum it up its a console application that reads information from a text file containg information on MP3's. So far i've written the class:

class MP3
          char * songtitle;
          char * artiste;
          int tracklength;
          MP3(char *, char *, int);
          const char * getartiste () const(return artiste;)
          const char * getsongtitle () const(return songtitle;)
          int tracklength const (return tracklength;)
          delete artiste;
          delete songtitle;
    MP3::MP3(char * newartiste, char * newsongtitle, int newtracklength)
    artiste = newartiste;
    tracklength = newtracklength;
    songtitle = newsongtitle;

Ive also worked out the loop to create the array of MP3's:

mp3 *arMP3[5]; //Create an array of pointers to 5 mp3 objects
for (nCount=0;ncount<5;nCount++)
arMP3[nCount] = new mp3;

But i'm struggling to work out how to write data from a text file into the 5 MP3's of the array. I know how to read information from a file into a simple array using this code:

ifstream myfile ("example.txt"); //opening the file.
    if (myfile.is_open()) //if the file is open
        while (! myfile.eof() ) //while the end of file is NOT reached
            getline (myfile,line); //get one line from the file
            array[loop] = line;
            cout << array[loop] << endl; //and output it
        myfile.close(); //closing the file

But i'm not sure how to write data from this file into the MP3 objects. Any help would be muchly appreciated. Thanks

MP3::MP3(char * newartiste, char * newsongtitle, int newtracklength)
    artiste = newartiste;
    tracklength = newtracklength;
    songtitle = newsongtitle;

The above code is a disaster piece. First thing, all you are doing is assigning the reference or the address of the C string passed in the function to the member variables of your object. Thus effectively, both your local function variables and object variables point to the same location, which is incorrect. Trying to free these variables adds another feather in the hat. Also you can't assign values to C style variables using assignment operators. You have got to use strcpy. Use variables of the string class to hold the song and artist name since you would be spared of the trouble of allocating and deallocating memory.

And as far as the issue of reading from the file is concerned, you can write values to the file in the CSV (comma seperated values) format. Artist name, Song name, Track Length While reading, read the entire string in a string variable and do selective processing which best suits your application needs.

Cheers for the help, incase you haven't guessed i'm really new to C++!! When you say selective processing for the string coulds you please give me an example. I tried looking for it but haven't had much joy. Cheers!

By selective processing I meant, split or process the string in such a way that it can be used for initializing the members or loading the class data.

Extracted string:
artist, track, time

Processed Output:
artist -> store in artist name
track -> store in track name
time -> store in track length

This article has been dead for over six months. Start a new discussion instead.