Question

For an ungraded C++ lab, we have practice with structs. I'm having a hard time calling...

For an ungraded C++ lab, we have practice with structs. I'm having a hard time calling the functions relating to our book struct correctly. The issues lie in main(). Here is the .cpp:

#include "book.h"


void add_author(Book& book){
        if (book.num_auth==3){
            std::cout<<"\nA book can't have more than 3 authors!";
        }
        else{
            std::cout<<"\nAdd Author: ";
            getline(std::cin, book.authors[book.num_auth]);
            book.num_auth++;
        }
};

void remove_last(Book& book){
        if(book.num_auth<=1){
            std::cout<<"\nA book must have at least 1 author!";
        }
        else{
            book.authors[book.num_auth-1]="";
            book.num_auth=book.num_auth-1;
        }
};

std::ostream& print(std::ostream& out, Book book){
        std::cout<<"\n\nTitle: "<<book.title<<"\nAuthors: ";
        for(int i=0; i<book.num_auth; i++){
            std::cout<<"\n\t"<<book.authors[i];
        }
        std::cout<<"\nGenre: "<<book.genre<<"\nPublisher: "<<book.publisher<<std::endl;
        return out;
};

void initializeBook(Book& book){
        std::cout<<"\nEnter the Title of the book: ";
        getline(std::cin, book.title);

        std::cout<<"\nGenre: ";
        getline(std::cin, book.genre);

        std::cout<<"\nPublisher: ";
        getline(std::cin, book.publisher);

        book.authors=new std::string[3];
        std::cout<<"\nFirst Author: ";
        getline(std::cin, book.authors[0]);
        book.num_auth=1;
}

int main(){

Book book;                                                                                                                       //QUESTION//what would this do?
std::string Book = initializeBook(book.title, book.genre, book.publisher, book.authors); //QUESTION//how do I call this function correctly?
add_author(Book& book);                                                                                               //QUESTION//same as above
remove_last(Book& book);                                                                                           //QUESTION//same as above


return 0;
};


and the .h:

#ifndef __BOOK_H_INCLUDED__
#define __BOOK_H_INCLUDED__
#include <iostream>

struct Book{
    std::string title;
    std::string genre;
    std::string publisher;
    int num_auth;
    std::string* authors;

};

void add_author(Book& book);
void remove_last(Book& book);
std::ostream& print(std::ostream& out, Book book);
void initializeBook(Book& book);

#endif

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Use this :

int main(){

Book book; //QUESTION//what would this do?

initializeBook(book); //QUESTION//how do I call this function correctly?

print(std::cout, book);

add_author(book); //QUESTION//same as above

remove_last(book); //QUESTION//same as above


return 0;

};

==================================
SEE OUTPUT

Files E saved main.cpp 48 } clang version 7.0.0-3-ubuntu0.18.04.1 > clangh-7 -pthread -o main main.cpp ./main main.cpp 49 50

PLEASE COMMENT if there is any concern.

=============================

Add a comment
Know the answer?
Add Answer to:
For an ungraded C++ lab, we have practice with structs. I'm having a hard time calling...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • please Code in c++ Create a new Library class. You will need both a header file...

    please Code in c++ Create a new Library class. You will need both a header file and a source file for this class. The class will contain two data members: an array of Book objects the current number of books in the array Since the book array is moving from the main.cc file, you will also move the constant array size definition (MAX_ARR_SIZE) into the Library header file. Write the following functions for the Library class: a constructor that initializes...

  • A library maintains a collection of books. Books can be added to and deleted from and...

    A library maintains a collection of books. Books can be added to and deleted from and checked out and checked in to this collection. Title and author name identify a book. Each book object maintains a count of the number of copies available and the number of copies checked out. The number of copies must always be greater than or equal to zero. If the number of copies for a book goes to zero, it must be deleted from the...

  • fully comments for my program, thank you will thumb up #include <iostream> #include <fstream> #include <string>...

    fully comments for my program, thank you will thumb up #include <iostream> #include <fstream> #include <string> #include <iomanip> using namespace std; struct book { int ISBN; string Author; string Title; string publisher; int Quantity; double price; }; void choice1(book books[], int& size, int MAX_SIZE) { ifstream inFile; inFile.open("inventory.txt"); if (inFile.fail()) cout <<"file could not open"<<endl; string str;    while(inFile && size < MAX_SIZE) { getline(inFile, str); books[size].ISBN = atoi(str.c_str()); getline(inFile, books[size].Title);    getline(inFile, books[size].Author); getline(inFile, books[size].publisher);          getline(inFile,...

  • Objectives: The main objective of this assignment is checking students’ ability to implement membership functions. After...

    Objectives: The main objective of this assignment is checking students’ ability to implement membership functions. After completing this assignment, students will be able to:  implement member functions  convert a member function into a standalone function  convert a standalone function into a member function  call member functions  implement constructors  use structs for function overloading Problem description: In this assignment, we will revisit Assignment #1. Mary has now created a small commercial library and has managed...

  • c++, I am having trouble getting my program to compile, any help would be appreciated. #include...

    c++, I am having trouble getting my program to compile, any help would be appreciated. #include <iostream> #include <string> #include <string.h> #include <fstream> #include <stdlib.h> using namespace std; struct record { char artist[50]; char title[50]; char year[50]; }; class CD { //private members declared private: string artist; //asks for string string title; // asks for string int yearReleased; //asks for integer //public members declared public: CD(); CD(string,string,int); void setArtist(string); void setTitle(string); void setYearReleased(int); string getArtist() const; string getTitle() const; int...

  • Fix my code, if I the song or the artist name is not on the vector,...

    Fix my code, if I the song or the artist name is not on the vector, I want to user re-enter the correct song or artist name in the list, so no bug found in the program #include <iostream> #include <vector> #include <string> #include <algorithm> using namespace std; class musicList{ private: vector<string> songName; vector<string> artistName; public: void addSong(string sName, string aName){ songName.push_back(sName); artistName.push_back(aName); } void deleteSongName(string sName){ vector<string>::iterator result = find(songName.begin(), songName.end(), sName); if (result == songName.end()){ cout << "The...

  • I created a struct for Car and now I need to turn it into a class...

    I created a struct for Car and now I need to turn it into a class for Car. Below is the code that I wrote for the structure. For the class, we are suppose to make the data in the class Car private, revise the input so the input function will only read the data from the user, and then it will call an additional function named setUpCar which will put the data into the object. Not sure how to...

  • C++ getline errors I am getting getline is undefined error messages. I would like the variables...

    C++ getline errors I am getting getline is undefined error messages. I would like the variables to remain as strings. Below is my code. #include <iostream> #include<string.h> using namespace std; int index = 0; // variable to hold how many customers are entered struct Address //Structure for the address. { int street; int city; int state; int zipcode; }; // Customer structure struct Customer { string firstNm, lastNm; Address busAddr, homeAddr; }; // Functions int displayMenu(); Customer getCustomer(); void showCustomer(Customer);...

  • my program wont run on my computer and im not understanding why. please help. #include<iostream> #include<iomanip>...

    my program wont run on my computer and im not understanding why. please help. #include<iostream> #include<iomanip> #include<string> #include "bookdata.h" #include "bookinfo.h" #include "invmenu.h" #include "reports.h" #include "cashier.h" using namespace std; const int ARRAYNUM = 20; BookData bookdata[ARRAYNUM]; int main () { bool userChoice = false; while(userChoice == false) { int userInput = 0; bool trueFalse = false; cout << "\n" << setw(45) << "Serendipity Booksellers\n" << setw(39) << "Main Menu\n\n" << "1.Cashier Module\n" << "2.Inventory Database Module\n" << "3.Report Module\n"...

  • #include #include #include #include #include #include // NOLINT (build/c++11) #include class Clock { private: std::chrono::high_resolution_clock::time_point start;...

    #include #include #include #include #include #include // NOLINT (build/c++11) #include class Clock { private: std::chrono::high_resolution_clock::time_point start; public: void Reset() { start = std::chrono::high_resolution_clock::now(); } double CurrentTime() { auto end = std::chrono::high_resolution_clock::now(); double elapsed_us = std::chrono::duration std::micro>(end - start).count(); return elapsed_us; } }; class books{ private: std::string type; int ISBN; public: void setIsbn(int x) { ISBN = x; } void setType(std::string y) { type = y; } int putIsbn() { return ISBN; } std::string putType() { return type; } }; std::ostream...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT