-3

I want display assigned values to object creating by class. I use **strcpy_s ** to default constructor and overload constructor in book.cpp I implement overloading constructor using strcpy_s prevoius object assigned values are displaying. Object created using book class not displaying get erro message I attach error message screen shot below.

book.h

#pragma once
#include "visitor.h"
#include "RegisteredUser.h"


class Books {
private:
     char bookId[10];
     char isbn[10];
     char title[30];
     char author[20];
     char publisher[20];
     char publicationDate[15];
     double price;
     char language[10];
     int noOfPages;
     Visitor* booksVisitor;
     RegisteredUser* user;

public:
    Books();
    Books(const char pbookId[], const char pisbn[], const char ptitle[], const char pauthor[], const char ppublisher[], const char ppublicationDate[], double pprice, const char planguage[], int pnoOfPages, RegisteredUser* puser, Visitor* pvisitor);
    void displayBookDetails();
    void storeBookDetails();
    void maintainBookInventory();
    ~Books();
};

book.cpp

#include <iostream>
#include "book.h"
#include "RegisteredUser.h"
#include "visitor.h"

using namespace std;



Books::Books()
{
    strcpy_s (bookId , "");
    strcpy_s (isbn , "");
    strcpy_s (title , "");
    strcpy_s (author , "");
    strcpy_s (publisher, "");
    strcpy_s (publicationDate, "");
    price = 0.00;
    strcpy_s (language, "");
    noOfPages = 0;

}

Books::Books(const char pbookId[], const char pisbn[], const char ptitle[], const char pauthor[], const char ppublisher[], const char ppublicationDate[], double pprice, const char planguage[], int pnoOfPages)
{
    strcpy_s(bookId, pbookId);
    strcpy_s(isbn, pisbn);
    strcpy_s(title, ptitle);
    strcpy_s(author, pauthor);
    strcpy_s(publisher, ppublisher);
    strcpy_s(publicationDate, ppublicationDate);
    price = pprice;
    strcpy_s(language, planguage);
    noOfPages = pnoOfPages;
    user = puser;
    booksVisitor = pvisitor;
}


void Books::displayBookDetails() {
    cout << "Book ID: " << bookId << endl;
    cout << "ISBN: " << isbn << endl;
    cout << "Title: " << title << endl;
    cout << "Author: " << author << endl;
    cout << "Publisher: " << publisher << endl;
    cout << "Publication Date: " << publicationDate << endl;
    cout << "Price: " << price << endl;
    cout << "Language: " << language << endl;
    cout << "Number of Pages: " << noOfPages << endl;
    user->displayRegisteredUserDetails();
    booksVisitor->display();
}

void Books::storeBookDetails() {

}

void Books::maintainBookInventory() {

}

Books::~Books()
{
}

main.cpp

// online-book-store.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include <iostream>
#include "visitor.h"
#include "RegisteredUser.h"
#include <cstring>
#include <string.h>
#include "book.h"

using namespace std;

int main()
{

    Visitor* Vi1 = new Visitor("Mihi", "2001/01/01", "Male", "150/B Panadura", 719562217);

    cout << "Visitor Details"<<endl;

    Vi1->display();

    RegisteredUser* r1 = new RegisteredUser("RU1234", "kavindu@gmail.com", "Kavindu01", "Mihi", "2001/01/01", "Male", "150/B Panadura", 719562217);

    cout << "Registred User Details "<<endl;

    r1->displayRegisteredUserDetails();

    Books* B01 = new Books("B001", "1020B9GT", "Madol Duwa", "Martin Wickramasinghe","Galle Pubs","21/09/2004",2000.00,"Sinhala", 220,Vi1,r1);

    B01->displayBookDetails();



}

@john updated book.h

#pragma once
#include "visitor.h"
#include "RegisteredUser.h"

class Books {
private:
    string bookId;
    string isbn;
    string title;
    string author;
    string publisher;
    string publicationDate;
    float price;
    string language;
    int noOfPages;
    Administrator* admin;
    Visitor* visitor;
    RegisteredUser* user;

public:

    Books(string pbookId, string pisbn, string ptitle, string pauthor, string ppublisher, string ppublicationDate, float pprice, string planguage, int pnoOfPages,RegisteredUser* puser, Visitor* pvisitor);
    void displayBookDetails();
    void storeBookDetails();
    void maintainBookInventory();
};

#include <iostream>
#include <iomanip>
#include <string>
#include <ostream>
#include "book.h"
#include "Admin.h"
#include "RegisteredUser.h"
#include "visitor.h"

using namespace std;

void Books::displayBookDetails() {
    cout << "Book ID: " << bookId << endl;
    cout << "ISBN: " << isbn << endl;
    cout << "Title: " << title << endl;
    cout << "Author: " << author << endl;
    cout << "Publisher: " << publisher << endl;
    cout << "Publication Date: " << publicationDate << endl;
    cout << "Price: " << price << endl;
    cout << "Language: " << language << endl;
    cout << "Number of Pages: " << noOfPages << endl;
    user->displayRegisteredUserDetails();
    visitor->display();
    admin->displayAdmin();
}

Books::Books(string pbookId, string pisbn, string ptitle, string pauthor, string ppublisher, string ppublicationDate, float pprice, string planguage, int pnoOfPages, Administrator* padmin, RegisteredUser* puser, Visitor* pvisitor) {
    bookId = pbookId;
    isbn = pisbn;
    title = ptitle;
    author = pauthor;
    publisher = ppublisher;
    publicationDate = ppublicationDate;
    price = pprice;
    language = planguage;
    noOfPages = pnoOfPages;
    user = puser;
    admin = padmin;
    visitor = pvisitor;
}

void Books::storeBookDetails() {

}

void Books::maintainBookInventory() {

}

I want to know the reason why is enter image description here error popin and solution for displaying object assigned values.

  • 1
    Please try to create a [mre] to show us, with emphasis on the **minimal** part. And use a [*debugger*](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) to help you figure out when and where in *your* code it happens. – Some programmer dude Aug 21 '23 at 19:08
  • 3
    As for a simple solution: Never use your own C-style null-terminated strings and arrays, always use `std::string` for all your strings. – Some programmer dude Aug 21 '23 at 19:09
  • Ok I will do that – Dishal Kanishka Aug 21 '23 at 19:15
  • Make absolutely certain that `strcpy_s ` does exactly what you want it to. It usually doesn't, so I almost never use it. – user4581301 Aug 21 '23 at 19:18
  • 2
    In any case you said `char author[20];` and that is not big enough for `"Martin Wickramasinghe"` which is exactly what the error message is telling you. – john Aug 21 '23 at 19:18
  • @john I change it to strings – Dishal Kanishka Aug 21 '23 at 19:18
  • @DishalKanishka Good idea. – john Aug 21 '23 at 19:19
  • @john Still getting errors – Dishal Kanishka Aug 21 '23 at 19:23
  • @DishalKanishka Well you need to show the new code and say what the new errors are. – john Aug 21 '23 at 19:23
  • @john I will give you github link can you solve that is it okay – Dishal Kanishka Aug 21 '23 at 19:24
  • @DishalKanishka Sorry I prefer to do things here. You can always ask a new question. – john Aug 21 '23 at 19:25
  • @john I update the new codes – Dishal Kanishka Aug 21 '23 at 19:26
  • `Visitor* Vi1 = new Visitor("Mihi", "2001/01/01", "Male", "150/B Panadura", 719562217);` -- There is no need to use `new` here. Just this: `Visitor Vi1("Mihi", "2001/01/01", "Male", "150/B Panadura", 719562217);` is all that should be done. It looks like you are using Java, C#, or Python as a model in writing C++ code. Don't do that. – PaulMcKenzie Aug 21 '23 at 19:34
  • @john I updated new book.h code is above – Dishal Kanishka Aug 21 '23 at 19:35
  • Where are you learning C++ from? It seems to be a bit of an outdated source. (Since it still uses raw pointers, "C" style arrays and useless `new`) – Pepijn Kramer Aug 21 '23 at 19:36
  • Good sources to learn cpp from are : A [recent C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) or have a go at https://www.learncpp.com/ (that's pretty decent, and pretty up-to-date). For C++ reference material use : [cppreference](https://en.cppreference.com/w/). And after you learned the C++ basics from those sources, look at the [C++ coreguidelines](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines) regularely to keep up-to-date with the latest guidelines. – Pepijn Kramer Aug 21 '23 at 19:37
  • @PepijnKramer I learnt from my institute – Dishal Kanishka Aug 21 '23 at 19:38
  • @DishalKanishka If you are using `std::string` you can no longer use `strcpy_s`, just use `=` instead. So replace `strcpy_s(bookId, pbookId)` with `bookId = pbookId` – john Aug 21 '23 at 19:39
  • Can you tell your institute to read those C++ code guidelines and tell them C++ has moved on and they should not teach C++ as it was before 1998. Even Bjarne asks teachers to stop teaching "C" when they teach "C++" – Pepijn Kramer Aug 21 '23 at 19:39
  • @john I change that correctly give me minute to update changed code – Dishal Kanishka Aug 21 '23 at 19:40
  • @DishalKanishka Please also include the latest error messages. – john Aug 21 '23 at 19:41
  • @john same error message even I deleted those book.h and book.cpp – Dishal Kanishka Aug 21 '23 at 19:43
  • @DishalKanishka That doesn't seem possible. There is no limit to how big a `std::string` can be, that's one of the advantages. – john Aug 21 '23 at 19:46
  • @PepijnKramer it was only recently that institutes in a large swath of south Asia stopped teaching their students TurboC++. Believe it or not, this is a win. – user4581301 Aug 21 '23 at 21:44

0 Answers0