gpt4 book ai didi

c++ - 程序在返回 cstring 时返回垃圾值

转载 作者:行者123 更新时间:2023-11-30 05:24:54 25 4
gpt4 key购买 nike

我的程序正在为建模为 C 字符串的变量返回垃圾值。该程序使用一个类来建模数据类型 Song。设置变量时似乎会发生错误。
下面列出了程序的输入和输出。
对于代码的长度,我深表歉意。

为什么是随机值?

代码

#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;
}

输入/输出

始终返回的输入和错误值:

 //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

最佳答案

问题出在您的 getter 成员函数中,您实际上返回了一个字符而不是数组。您需要进行以下更改:

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;
}

关于c++ - 程序在返回 cstring 时返回垃圾值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38534542/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com