-7

I am tasked to create a Print function that prints user inputted data that is specific to an object. This print function must use the Get() Function commands I created.

I have googled and looked for similar questions but could not find a way of how I could approach this. How can I create this function my teacher wants?

The object I want to print specifically is book1

My code:

     #include <iostream>
     #include <string>
     #include <cstdio>
     using namespace std;

       class Book {
       public:
       void SetTitle(string title_input);
       string GetTitle();
       void SetAuthor(string& author_input);
       string GetAuthor();
       void SetCopyRightYear(int copyright_year_input);
       int GetCopyRightYear();
       void PrintBook();

       private:
       string title;
       string author;
       int copyright_year;
    };

     void Book::SetTitle(string title_input) {
           title = title_input;
       }
         string Book::GetTitle() {
             return title;
         }
         void Book::SetAuthor(string& author_input) {
             author = author_input;
         }
         string Book::GetAuthor() {
             return author;
         }
         void Book::SetCopyRightYear(int copyright_year_input) {
             copyright_year = copyright_year_input;
         }
         int Book::GetCopyRightYear() {
             return copyright_year;
         }
         void Book::PrintBook() {
             cout << "Title of Book: " << GetTitle() << endl;
             cout << "Author of Book: " << GetAuthor() << endl;          // Function is broken FIXME
             cout << "Copyright Year: " << GetCopyRightYear() << endl;
         }


    int main ()
    {
        string title_input = "";
        string author_input = "";
        int copyright_year_input = 0;


        Book book1;
        Book book2;
        Book book3;
        Book book4;

        cout << "Enter the book title: ";
        cin >> title_input;
        book1.SetTitle(title_input);
        cout << book1.GetTitle();
        cout << "Enter the author name: ";
        cin >> author_input;
        book1.SetAuthor(author_input);
        cout << "Enter the copyright year: ";
        cin >> copyright_year_input;
        book1.SetCopyRightYear(copyright_year_input);

        cout << PrintBook();
test test
  • 1
  • 1
  • 4
    What is your problem? What is your input? What is your actual output? What is your expected output? *What is your question?!* Please [read about how to ask good questions](http://stackoverflow.com/help/how-to-ask), and learn how to create a [Minimal, **Complete**, and Verifiable Example](http://stackoverflow.com/help/mcve). – Some programmer dude Oct 16 '17 at 07:15
  • Your `PrintBook()` function looks fine according to your requirement. What's not working actually? – user0042 Oct 16 '17 at 07:17
  • Running /home/ubuntu/workspace/lab25/lab25.cpp /home/ubuntu/workspace/lab25/lab25.cpp: In function ‘int main()’: /home/ubuntu/workspace/lab25/lab25.cpp:75:31: error: ‘PrintBook’ was not declared in this scope cout << PrintBook(); ^ This is the error I am recieving – test test Oct 16 '17 at 07:19
  • You know how to call a member function like `SetTitle`, but not another member function of the same object named `PrintBook`? – Some programmer dude Oct 16 '17 at 07:20
  • We cant see printbook () inside ur main – Rostin Oct 16 '17 at 07:20
  • `scope cout << book1.PrintBook();` you are missing class instance to invoke `PrintBook` non-static member function. – user7860670 Oct 16 '17 at 07:20
  • My problem is that I need a print function that can print my Get() functions. The inputs are any strings or a number. The outputs should be that. The print function should specifically print the object book1. – test test Oct 16 '17 at 07:22
  • 1
    Read my second comment again. How can you know how to use member functions, and at the same time *not* know how to use member functions? Did you write the program? Did you write the expressions `book1.SetTitle(title_input)` and `cout << book1.GetTitle()`? How do you *think* you should call the member function `PrintBook` on the object `book1`? – Some programmer dude Oct 16 '17 at 07:25
  • I know how to call SetTitle yes but I don't know to get PrintBook to output the data I want using my Get() functions Some progrmmer dude. – test test Oct 16 '17 at 07:25
  • 2
    A member function is a member function is a member function! You call *all* member functions in a similar way. And `PrintBook` ***is*** a *member function!* Think! Use your brain! [Read books](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list)! – Some programmer dude Oct 16 '17 at 07:28
  • I did write the program and the expressions. That is not my issue. Yes I can write book1.PrintBook. But my PrintBook function does not work. It needs to use the Get() functions to call from book1 so it can print the outputs. But if I code in my PrintBook function book1.GetTitle() or any of the of the get functions I get an error. – test test Oct 16 '17 at 07:28
  • Oh wait, you try to print what `book1.PrintBook()` *returns*? But that function *doesn't* return anything. It prints by itself! Just *call* it. This is the reason you should show us the complete program *and* the errors in the question body. We ***need*** it to see what's really happening. Now go back to my *first* comment, and follow the links! – Some programmer dude Oct 16 '17 at 07:30
  • How do I call it if book1.PrintBook() isnt what works.? – test test Oct 16 '17 at 07:33
  • Simply change `cout << PrintBook();` to `book1.PrintBook();`. It works fine here. – Jabberwocky Oct 16 '17 at 08:19
  • In one of your comment you write: _But if I code in my PrintBook function book1.GetTitle() or any of the of the get functions I get an error_. When __exactly__ does this error occur? What error message is displayed __exactly__? – Jabberwocky Oct 16 '17 at 08:23

1 Answers1

0

Book.h

#pragma once
#include <string>

class Book
{
public:

    Book() = default;
    ~Book() = default;

    const std::string GetTitle() const;
    const std::string GetAuthor() const;
    const int GetCopyRightYear() const;

    void SetTitle(const std::string);
    void SetAuthor(const std::string);
    void SetCopyRightYear(const int);
    void PrintBook();

private:
    std::string title;
    std::string author;
    int copyright_year;
};

Book.cpp

#include "Book.h"
// ------------------------------
#include <iostream>




void Book::SetTitle(const std::string title_input)
{
    title = title_input;
}



const std::string Book::GetTitle() const
{
    return title;
}



const int Book::GetCopyRightYear() const
{
    return copyright_year;
}



const std::string Book::GetAuthor() const
{
    return author;
}



void Book::SetCopyRightYear(const int copyright_year_input)
{
    copyright_year = copyright_year_input;
}



void Book::SetAuthor(const std::string author_input)
{
    author = author_input;
}



void Book::PrintBook()
{
    std::string output_str = "";
    std::cout << "Title of Book: " << GetTitle() << std::endl;
    std::cout << "Author of Book: " << GetAuthor() << std::endl;
    std::cout << "Copyright Year: " << GetCopyRightYear() << std::endl;
}

main.cpp

// C++ Libraries.
#include <iostream>
#include <string>

// User classes
#include "Book.h"

// Namespaces

int main()
{
    std::string title_input = "";
    std::string author_input = "";
    int copyright_year_input = 0;

    // research dynamic memory allocation.
    Book book1;
    Book book2;
    Book book3;
    Book book4;


    // user sets book title.
    std::cout << "Enter the book title: ";
    std::getline(std::cin, title_input);
    book1.SetTitle(title_input);

    // user sets the authors name
    std::cout << "Enter the author name: ";
    std::getline(std::cin, author_input);
    book1.SetAuthor(author_input);

    // user inputs the copyright year.
    std::cout << "Enter the copyright year: ";
    std::cin >> copyright_year_input;
    book1.SetCopyRightYear(copyright_year_input);

    // Display the information.
    book1.PrintBook();
}

Notes:

  • When you start using multiple namespaces its easier to see what is what if you dont predefine them.
  • Const correctness means you and other developers know what can be changed and what cant. It also makes things clearer for the compiler.
  • std::getline reads the whole line including the blank spaces.

Just a quick note on clarity and understanding. At the moment your code is messy which makes it incredibly hard to debug not only for yourself but for others.

I can't tell on here but just in case, your classes should be in header and source code formatting, with a main source code file for the main function (entry point). Whether or not you've been told this information before I would highly recommend doing some research into basic C++. Just for starters I've put some links below to help. Once your code is neatly formatted you might work out what the problem is.

Happy coding :)

References: Herb Sutter Cpp Convention 2014 - Simplicity over Complexity: https://www.youtube.com/watch?v=xnqTKD8uD64

Headers and Includes - C++ formatting: http://www.cplusplus.com/forum/articles/10627/

Also see the tutorials on cplusplus.com.

DDaqes
  • 11
  • 3