qtngmin,
Do you mean a segmentation fault? If so, which one? There are several, 11 is a common one– it means your program is trying to access memory that it shouldn't.
Also–
PLEASE USE CODE TAGS (the <> formatting button to the right of this box), when posting code.
Along with the proper indenting, it makes it easier to read your code, and thus also easier to respond to your post.
Hint: You can hit "edit post", highlight your code and then press the <> formatting button. This will not automatically indent your code. That part is up to you.
You can use the "preview" button at the bottom to see how it looks.
I will look at your code and see if I can find the problem. Try debugging it and see if you can find the issue with that.
Luck,
max
Edit:
Here is your code using code tags and better formatting:
thanks for your reply. Im a vietnamese so its hard for me to understand, sorry if its border you . The input is here
Book book1("Giai tich 1", "Nguyen Dinh Huy, Nguyen Thi Xuan Anh", 2000);
Printer::printBook(book1);
output as it shoud be
Giai tich 1
Nguyen Dinh Huy
Nguyen Thi Xuan Anh
2000
and i got this message
***Error***
Segmentation fault (core dumped)
I cant see where the error is because it is online submit, and i compile it online too
Offhand I'd say your problem is very likely in the Book overloaded constructor, where you have all the this pointers and other pointers.
Do you have a program that uses this class? If so, it would be nice if you would post it here so we don't have to write our own. It just makes it easier for us.
No i dont have, it just like a quiz online, all the code is hide from the student, we write the code in the Book class to complie it online a unlimited submit. That is all i have
Book (constchar *title, constchar *authors, int publishingYear)
{
*this -> title = *title; // A single character is copied to invalid memory
*this -> authors = *authors; // A single character is copied to invalid memory
this -> publishingYear = publishingYear;
}
And this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
staticvoid printBook (const Book book) // Better use const reference instead of copy: const Book& book
{
std::cout << book.title << std::endl;
char *tmp = newchar[100]; // Memory is allocated....
tmp = book.authors; // The pointer is replaced with book.authors
while (tmp != NULL)
{
std::cout << tmp << std::endl;
}
delete tmp; // book.authors memory is deleted
std::cout << book.publishingYear << std::endl;
}
I dont know how to explain, the question said it is more than one author for 1 book, and the input has 2 names (in vietnamese). Here is the input example given in the test:
Book book1("Giai tich 1", "Nguyen Dinh Huy, Nguyen Thi Xuan Anh", 2000);
Printer::printBook(book1);
Output:
Giai tich 1
Nguyen Dinh Huy
Nguyen Thi Xuan Anh
2000
is there anyway to catch a " , " from input file and change it to "\n " . because authors in the input is several, and each name has to be on one line in the output. Anyways thank you guys for helping me
printBook() iterates through the book string displaying each char until a , is found. It then outputs a newline and skips the , and following spaces. This repeats until the end of the string is found.
Book(const Book &book){ //answer}
I have another issue here too, is this likely to be an operator overload function? Can you guys help me with this one too.
About the previous code you give, I know what you did but it is hard for me to understand what is in the code. This is my first time learning OOP , i ll try my best
In my country we dont have much source about programming, i have to search everything in another language , it confusing me a lot, but at least i got help, that means a lot
Yes. I marked that as =delete in my code above, but the proper code needs to be provided for that to work. It's simply a deep copy of the class members - similar to what was done for the existing constructor. Instead of title_, author_ etc, these come from the function param book.
Ok, is there any e-book for this learning, can you show me. I need help with the code but still want to understand i write it again as my exp, for sure I have assignment about it later