C++ help please :( Any help??

Soldato
Joined
17 Jul 2005
Posts
3,193
Hi all,

Hate C++, but have to do it as a module soooo....

Trying to make a CLI based media player, and I need a MP3, Streamer & CD player... anyway, i'm trying to start on the Mp3 player.

I need to basically input the following from a file;

Title, Artist, Length

At the moment i'm just trying to create the class, and instantiate a few and add them to a vector!

The code i've got is;

mp3.h said:
//Class File

#include <vector>

using namespace std;

class Mp3
{
public:

char songTitle[30];
char songArtist[30];
int songLength;
int currentPos;


Mp3(char *title, char *artist, int length, int pos)
{
strcpy(songTitle,title);
strcpy(songArtist,artist);
songLength = length;
currentPos = pos;
}

void print(void)
{
cout << songTitle;
}



};

And... the main;

main.cpp said:
#include <cstdlib>
#include <iostream>
#include <vector>

using namespace std;

#include "mp3.h"

int main()
{
vector<Mp3> v();
char title, artist;
int length, pos;

while (cin >> title >> artist >> length >> pos)
{
v.push_back(Mp3(title, artist length, pos));
};

system("PAUSE");
}

So basically i've created the class, and then in main i've created the four variables again so I can add them to the vector but using the class structure... anyone understand? :s

I've been using code bodged from everwhere, but i'm trying to get little thigns working individually because its getting messy in my actual program.

ANY HELP would be apprecated, i'm more into my networking :o
 
managed to sort this now :o

just got to get it working from a file.

What's the best way to copy lines from a file into a vector using the above structure?
 
Last edited:
lol, what uni do you go to? Staffordshire by any chance? :p

Add me on MSN if you want, I'm quite far into the assignment :)
 
Hey welshy, i'm off out now but I might add you if no one can answer :p try to stay off msn as much as poss or else nothing gets done :o

main.cpp said:
#include <cstdlib>
#include <iostream>
#include <vector>
#include <fstream>

using namespace std;

#include "mp3.h"

int main()
{
char songTitle[30];
char songArtist[30];
int songLength;


vector<Mp3> v;

ifstream mp3l("H:\\mp3l.txt");


while (!mp3l.eof())
{
mp3l >> songTitle >> songArtist >> songLength;
v.push_back(Mp3(songTitle, songArtist, songLength));
}

cout << v[0].songTitle << endl;


system("PAUSE");
}
I've managed to get it copying the file into the vector, and I can access it using the statement in bold above, everything has copied fine and it seems to be using the class above. (Class is the same as the one I posted in the OP)

Anyway, I want to make (just to test) a printDetails(); method in my mp3 class. So I did so;

IN THE HEADER FILE said:
void print(void)
{
cout << "Song Details: " << songTitle << songArtist << songLength;

}

How would I access this class method in my main? It's weird, usually when I instantiate a class object, I do;

Mp3 Song1(); etc...

so that object is called Song1... but now i've written a load from a file, so what would they be called if I wanted to use the Print function? :s

e.g ... mp31.print();

STUCK!
 
How do you not know how to access member functions? :confused:

There's one glaring problem you've got. Your loading from file using a while loop in the "main" class, when you really want to load the MP3s into a Vector your Cabinet class.

If you do go to my uni, then you must have done at least one java module, so you can't know that little about OOP? Can you? :p
 
Welshy said:
How do you not know how to access member functions? :confused:

There's one glaring problem you've got. Your loading from file using a while loop in the "main" class, when you really want to load the MP3s into a Vector your Cabinet class.

If you do go to my uni, then you must have done at least one java module, so you can't know that little about OOP? Can you? :p

Yep :p

Never really paid much attention to programming modules tbh :(... I'm into my hardware and networks too much :p I'm on compuTING science, and didn't want a sole networking degree... so opted to stay with this because I think it's worth more.

I think if you could explain what the cabinet class entailed, then I'd probably understand more? I got kind of confused with that today when I sat down and looked at *** assignment, what does it mean? I google cabinet classes and it doesn't bring much up... and I really can't follow the lecturers.

Any help would be appreciated m8, just need some pointers if you could give em?

Cheers.
 
Can't really explain it any better than the assignment to be honest. The cabinet class isn't a standard, it's just how he's explaining it.

I'll try and explain it in a nutshell:

Cabinet class:
Holds vectors/containers of CDs/MP3s/Streams loaded from file.
Holds instances of MP3Player, CDPlayer, StreamPlayer.

MP3Player:
Vector of MP3s, passed into the object in the constructor.

CDPlayer:
Vector of CDs, passed into the object in the constructor.

StreamPlayer:
Vector of Streams, passed into the object in the constructor.

When your loading from file, you just need to set the member variables to the titles/artists/lengths loaded from file, it doesnt affect the variable name.


Sounds like you need to get your tutor to go through it with you in your tutorial.
 
Few comments;

The attributes of your mp3 class - artist, title, etc, should be private, with getter functions.

If you want to read data from a file you should write a constructor for Mp3 that takes an istream and extracts the data.

So, for instance, you would have:

Code:
while(istream) {
   v.push_back(new Mp3(istream));
}

That way your main class doesnt need to know about the format of the fikle - its encapsulated within the Mp3 class.

Similarly you should define an oustream operator to write details of an Mp3 to file - this is analogous to providing a toString operator within Java. The reason for this is that if you added a field to Mp3 (such as 'genre') then you can provide methods to handle that within Mp3, rather than having to duplicate code every time you read/write an instance of Mp3 to a file.
 
Back
Top Bottom