question about classes

This is a discussion on question about classes within the C++ Programming forums, part of the General Programming Boards category; I've written a class that is supposed to add songs to a cd track. I need to keep track of ...

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    9

    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.

  2. #2
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    post some code... maybe your calling your constructor wrong? try an unsigned int...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  3. #3
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    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))
    Away.

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    9
    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
    };
    Last edited by tsut; 09-11-2003 at 07:57 PM.

  5. #5
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    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;
    }
    Last edited by major_small; 09-11-2003 at 08:08 PM.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    9
    Thanks! I got my progrma to run.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Classes question...
    By Raigne in forum C++ Programming
    Replies: 24
    Last Post: 09-12-2006, 01:46 AM
  2. Replies: 2
    Last Post: 07-28-2006, 02:59 PM
  3. Simple Question about Classes
    By Loctan in forum C++ Programming
    Replies: 5
    Last Post: 06-26-2006, 02:40 AM
  4. Classes and Win32 API, and another Question
    By philvaira in forum Windows Programming
    Replies: 10
    Last Post: 04-10-2004, 07:21 PM
  5. Newbie question about the Private type in classes
    By Ryeguy457 in forum C++ Programming
    Replies: 1
    Last Post: 09-07-2002, 10:17 PM

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