-
question about classes
I've written a class that is supposed to add songs to a cd track. I need to keep track of the number of songs added to the compact disc so I'm trying to implement a counter within a member function called AddTrack. I've set the counter to zero in the constructor - like other variables, but for some reason my counter starts out with a negative value even though I've set it to zero. Am I placing the counter in the right place? I have it declared in my private class.
Thanks for any suggestions.
-
post some code... maybe your calling your constructor wrong? try an unsigned int...
-
What type of variable is your counter? (Please say unsigned int or int, or even char) ((if you can't say that now, change it and then say it))
-
I am having problems with the variable named tot_time. I've used cout statements to debug and that's the only variable that is not initializing to zero. It's giving me something like 27000.
Do I need to keep the counter somewhere else? Maybe as a function somewhere? I've looked at this too long and I am stuck.
Thanks for any help or suggestions.
Here's my .cpp file.
Code:
// Constructor that initializes class CompactDisc
CompactDisc::CompactDisc() {
// initialize members
name = " ";
title = " ";
artist = " ";
seconds = 0;
no_of_songs = 0;
tot_time = 0;
track = 0;
total = 0;
}
// Function that adds track to cd
int CompactDisc::AddTrack(String na, int sec) {
seconds = sec;
name = na;
tot_time = tot_time + seconds;
if ((tot_time >= 4440) || (no_of_songs > 10)) {
tot_time = tot_time - seconds;
return 0;
}
else {
no_of_songs += 1;
array[no_of_songs] = name;
array_sec[no_of_songs] = seconds;
return 1;
}
}
Here's my .h file.
Code:
class CompactDisc {
public:
CompactDisc(); //constructor
CompactDisc(String, String); //constructor that takes artist and title of album
int AddTrack(String, int); //adds track and returns value of 1 if doesn't exceed limits
void PrintTrack(int); // accepts track number to print
int TrackCount(); // returns # of tracks added to cd
int track; // counter for track #
int total;
private:
String title; //title of album
String artist; //artist of album
String name; //name of song
int seconds; //time
String array [10]; // array to store songs for corresponding track #
int array_sec[10]; // array to store times for each song
int tot_time; // counter for total time on compact disc
int no_of_songs; // index for array that holds number of songs on compact disc
};
-
i just took a quick look... i can't see anything wrong except you didn't include the h file in the cpp file... looks like it should be working, unless i didn't see something or it has to do with some other code...
Code:
CompactDisc::CompactDisc() {
// initialize members
name = " ";
title = " ";
artist = " ";
seconds = 0;
no_of_songs = 0;
tot_time = 0;
track = 0;
total = 0;
}
try this:
Code:
// Constructor that initializes class CompactDisc
CompactDisc::CompactDisc() {
// initialize members
name[0] = NULL;
title[0] = NULL;
artist[0]= NULL;
seconds = 0;
no_of_songs = 0;
tot_time = 0;
track = 0;
total = 0;
}
-
Thanks! I got my progrma to run. :)