簡體   English   中英

類函數中的c ++ char數組輸出

[英]c++ char array output in class functions

我是一名真正的c ++初學者,我在c ++練習中的char數組輸出有問題。 我被要求將某個UML類轉換為c ++,並使用main中給定的參數生成有效的輸出。 這里的代碼:

#include <iostream>
#include <stdlib.h>


/*My class defintion book*/

class Book
{   protected: 
        long int number; 
        char author[25];
        int year;
        bool lent;

        void setLent(bool x);
        bool getLent(); 
    public: 
        Book(long int n, char a[25], int j, bool x);
        long int getNr();
        int getYear();
        void print();
        };
/*Method definition Book*/
Book::Book(long int n, char a[25], int j, bool x)
    {number=n;
    author=a;
    year=j;
    lent=x;}

long int Book::getNr()
    {return number; }

int Book::getYear()
    {return year;}

void Book::setLent(bool x)
    {lent=x;}

bool Book::getLent()
    {return lent;}

void Book::print()
    {
    std::cout << "Book Nr: " << number << std::endl;
    std::cout << "Author: " << author << std::endl;
    std::cout << "Year: " << year << std::endl;
    if (lent==0)
    std::cout << "Lent [yes/no]: no" << std::endl;
    else
    std::cout << "Lent [yes/no]: yes" << std::endl;
    }

/*MAIN*/

int main()
{
Book b1(123456, "test", 2014, false);

b1.print();

system("pause");
return 0;

這是我的輸出:

Book Nr: 123456
Author: b<Vv-[[vóYA
Year: 2014
Lent [yes/no]: no
Press any key to continue...

如您所見,除“作者”以外,所有輸出均有效。 那里我胡扯。 請注意,我必須使用char作為類型。 因為它是在UML類中給出的,所以我不得不轉換為c ++。

我真的到處搜尋。 但是沒有找到正確的解決方案。 我覺得這將是一個非常簡單的...

在此先感謝您的幫助!

您正在打印未初始化的數據。

使作者成為字符串

#include <string>
class Book
{   protected: 
        long int number; 
        std::string author;
        int year;
        bool lent;

並將構造函數的參數也設置為字符串

Book::Book(long int n, const std::string& a, int j, bool x)

字符數組不如std :: strings靈活。 它們只是數據的一部分。 如果要使用字符串,請改用std::string

另外,請在C ++構造函數中使用初始化列表,而不要在Java樣式中使用

Book::Book(long int n, const std::string &a, int j, bool x)
    : number(n),
    author(a),
    year(j),
    lent(x)
{ }

這樣做不起作用的原因是,您將指針 author分配給另一個指針 a ,然后該指針超出了范圍...因此,您留下的author指向一些垃圾。 如果你想堅持用字符數組,你必須復制所有數據a點:

strcpy(author, a);    

但是由於它是C ++,所以您應該只使用字符串,它更易於處理:

class Book {
    ...
    std::string author;
    ....
};

Book::Book(long int n, const std::string& a, int j, bool x)
: author(a), ...
{ }

您的代碼中有兩個錯誤:

Book::Book(long int n, const char a[25], int j, bool x)
{
    number=n;
    strncpy(author, a, 25);  // author = a;  doesn't work! shouldn't compile either...
    year=j;
    lent=x;
}

第一:變量作者是指向零終止字符串的指針。 您可以使用strcpy()復制此字符串。 因此,您需要#include <memory.h 但是,您需要確保字符串-確實是零終止的並且適合您的目標變量! 否則,您將覆蓋目標變量旁邊的其他內存區域,這也稱為緩沖區溢出! 最好使用strncpy(target,source,maxlength); 避免了這個問題。

第二:您的參數a應該是“ const”,因為您希望能夠像Book b1(123456, "test", 2014, false);那樣使用字符串常量來調用它Book b1(123456, "test", 2014, false); 其中"test"是常數!

正如其他人已經建議的那樣,您應該使用std::string而不是a[25] C字符串是“ C”而不是“ C ++”,因此您應盡量避免使用它們。 C字符串會在您的代碼中引入很多錯誤,並導致緩沖區溢出(=安全問題)。 而且它們處理起來更復雜。 您需要#include <string>才能使用它們。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM