[英]c++ char array output in class functions
I am a real c++ beginner and I have a problem with my char array output in a c++ excerise. 我是一名真正的c ++初学者,我在c ++练习中的char数组输出有问题。 I was asked to transform a certain UML class in to c++ and generate an working output with the parameters given in main.
我被要求将某个UML类转换为c ++,并使用main中给定的参数生成有效的输出。 Here ist the code:
这里的代码:
#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;
This is my output: 这是我的输出:
Book Nr: 123456
Author: b<Vv-[[vóYA
Year: 2014
Lent [yes/no]: no
Press any key to continue...
As you can see all outputs work except for the "Author". 如您所见,除“作者”以外,所有输出均有效。 There I am getting crap.
那里我胡扯。 Note that I have to use char as type.
请注意,我必须使用char作为类型。 since it is given in the UML class I had to transform into c++.
因为它是在UML类中给出的,所以我不得不转换为c ++。
I really searched everywhere. 我真的到处搜寻。 But didn't find the correct solution.
但是没有找到正确的解决方案。 I have the feeling it will be a very simple one...
我觉得这将是一个非常简单的...
Thanks in advance for your help! 在此先感谢您的帮助!
You are printing out uninitialized data. 您正在打印未初始化的数据。
Make author a string 使作者成为字符串
#include <string>
class Book
{ protected:
long int number;
std::string author;
int year;
bool lent;
and make the argument to the constructor a string as well 并将构造函数的参数也设置为字符串
Book::Book(long int n, const std::string& a, int j, bool x)
Arrays of characters are not as flexible as std::strings. 字符数组不如std :: strings灵活。 they are just chunks of data.
它们只是数据的一部分。 If you want to use strings then use
std::string
instead. 如果要使用字符串,请改用
std::string
。
Also, use an initializer list in C++ constructors, not java style 另外,请在C ++构造函数中使用初始化列表,而不要在Java样式中使用
Book::Book(long int n, const std::string &a, int j, bool x)
: number(n),
author(a),
year(j),
lent(x)
{ }
The reason this doesn't work is that you're assigning your pointer author
to another pointer a
, which then goes out of scope... so you're left with author
pointing to some garbage. 这样做不起作用的原因是,您将指针
author
分配给另一个指针 a
,然后该指针超出了范围...因此,您留下的author
指向一些垃圾。 If you want to stick with character arrays, you'll have to copy all the data that a
points to: 如果你想坚持用字符数组,你必须复制所有数据
a
点:
strcpy(author, a);
But since it's C++, you should just use strings, which are easier to deal with: 但是由于它是C ++,所以您应该只使用字符串,它更易于处理:
class Book {
...
std::string author;
....
};
Book::Book(long int n, const std::string& a, int j, bool x)
: author(a), ...
{ }
There are two bugs in your code: 您的代码中有两个错误:
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;
}
First: The variable author is a pointer to a zero terminated string. 第一:变量作者是指向零终止字符串的指针。 You can use strcpy() to copy this string.
您可以使用strcpy()复制此字符串。 Therefore you need to
#include <memory.h
. 因此,您需要
#include <memory.h
。 But you need to be sure that the string -is- really zero-terminated and fits into your target variable! 但是,您需要确保字符串-确实是零终止的并且适合您的目标变量! Else you'll overwrite other memory regions next to the target variable, which is also called a buffer overflow!
否则,您将覆盖目标变量旁边的其他内存区域,这也称为缓冲区溢出! Better use strncpy(target, source, maxlength);
最好使用strncpy(target,source,maxlength); which avoids this problem.
避免了这个问题。
Second: Your parameter a should be "const" as you want to be able to call it with a string constant like in Book b1(123456, "test", 2014, false);
第二:您的参数a应该是“ const”,因为您希望能够像
Book b1(123456, "test", 2014, false);
那样使用字符串常量来调用它Book b1(123456, "test", 2014, false);
where "test"
is a constant! 其中
"test"
是常数!
As others already suggested you should use std::string
instead of a[25]
. 正如其他人已经建议的那样,您应该使用
std::string
而不是a[25]
。 C-Strings are "C" and not "C++" and you should try to avoid them. C字符串是“ C”而不是“ C ++”,因此您应尽量避免使用它们。 C-Strings can introduce a lot of bugs into your code and enable buffer overflows (=security problems), too.
C字符串会在您的代码中引入很多错误,并导致缓冲区溢出(=安全问题)。 Also they are more complicated to handle.
而且它们处理起来更复杂。 You need to
#include <string>
to use them. 您需要
#include <string>
才能使用它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.