1

My program is returning garbage values for variables modeled as c-strings. The program uses a class to model datatype Song. It appears the error would occur when the variable is set.
Inputs to the program and the outputs are listed below.
My apologies for the length of the code.

Why the random values?

Code

#include <iostream>
#include <cstring>

using namespace std;

const int MAX_CHAR = 100;

class Song
{
public:
    //Default constructor
    Song();
    //Constructor
    Song(char title[], char artist[], char album[], int min, int sec);
   //Destructor
   ~Song();

   //Accessor functions
   char getTitle()const;
   char getArtist() const;
   char getAlbum() const;
   int getMin() const;
   int getSec() const;

   //Mutator functions
   void setTitle(const char*);
   void setArtist(const char*);
   void setAlbum(const char*);
   void setMin(int);
   void setSec(int);

private:
    //Member Variables
    char title[MAX_CHAR];
    char artist[MAX_CHAR];
    char album[MAX_CHAR];
    int min;
    int sec;
};

Song::Song(){}

Song::Song(char newTitle[],char newArtist[],char newAlbum[], int     newMin, int newSec)
{

    strncpy(title, newTitle, 100);
    strncpy(artist, newArtist, 100);
    strncpy (album, newAlbum, 100);
    min = newMin;
    sec = newSec;
}


Song::~Song(){}


//getter functions
char Song::getTitle() const
{
    return title[MAX_CHAR];
}

char Song::getArtist() const
{
    return artist[MAX_CHAR];
}

char Song::getAlbum() const
{
    return album[MAX_CHAR];
}

int Song::getMin() const
{
    return min;
}

int Song::getSec() const
{
    return sec;
}

//setter functions

void Song::setTitle(const char newTitle[])
{
    strcpy(title, newTitle);
}

void Song::setArtist(const char newArtist[])
{
    strcpy(artist, newArtist);
}

void Song::setAlbum(const char newAlbum[])
{
    strcpy(album, newAlbum);
}
void Song::setMin(int min)
{
    this->min = min;
}

void Song::setSec(int sec)
{
    this->sec = sec;
}

int main ()
{
    char newTitle[MAX_CHAR];
    char newArtist[MAX_CHAR];
    char newAlbum[MAX_CHAR];
    int min;
    int sec;

    cout << "Enter title: ";
    cin >> newTitle;
    cout << "Enter artist: ";
    cin >> newArtist;
    cout << "Enter album: ";
    cin >> newAlbum;
    cout << "Enter minutes: ";
    cin >> min;
    cout << "Enter seconds: ";
    cin >> sec;

    Song song;

    song.setTitle(newTitle);
    song.setArtist(newArtist);
    song.setAlbum(newAlbum);
    song.setMin(min);
    song.setSec(sec);

    cout << endl << "Title: " << song.getTitle() << endl <<
    "Artist: " << song.getArtist() << endl <<
    "Album: " << song.getAlbum() << endl <<
    "Minutes: " << song.getMin() << endl <<
    "Seconds: " << song.getSec() << endl;

    return 0;
}     

I/O

The inputs and the erroneous values that are consistently returned:

 //Inputs
    Enter title: title
    Enter artist: artist
    Enter album: album
    Enter minutes: 3
    Enter seconds: 20


    //Outputs
    Title: a
    Artist: a
    Album: 
    Minutes: 3
    Seconds: 20
6
  • What do you see when debugging it? Could you please provide a minimal reproducible example so we can help you out? Commented Jul 22, 2016 at 20:13
  • 1
    Just use std::string and get rid of all the arrays. Commented Jul 22, 2016 at 20:14
  • 1
    return title[MAX_CHAR]; -- Explain what you think this does. Commented Jul 22, 2016 at 20:15
  • @PaulMcKenzie It's supposed to return the cstring to the class, but that's probably also the bug. I don't think arrays can be returned, but see no other option. Commented Jul 22, 2016 at 20:17
  • @0x1000001 C++ is not a language where you are makiing guesses as to what each line of code you wrote does. You have to know what each and every line you happen to write is supposed to do. If not, then write a small program and get familiar with what you're trying to learn, instead of writing whole classes and not be sure of the most fundamental of issues. And no, that line does not return the entire string. Commented Jul 22, 2016 at 20:19

1 Answer 1

1

The problem is in your getter member functions where you actually return a character and not the array. You need to do the following changes:

const char* getTitle() const;
const char* getArtist() const;
const char* getAlbum() const;

const char* Song::getTitle() const {
   return title;
}

const char* Song::getArtist() const {
   return artist;
}

const char* Song::getAlbum() const {
   return album;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.