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
to collect sales data. She wants to be able to print the books
based on sales, and the list of authors based on their date of
birth. This assignment needs the structs Book, Author, Date, and
Library.
Processing inventory: She wants to display the books by popularity, thus the program must print the books based on sales, from highest to lowest. You will need to save the output into a text file named OrderedBooks.txt. Utilize the Book struct to implement the desired functionality.
Similarly, Mary is interested in displaying the authors by age, printing the oldest authors first. An output filed OrderedAuthors.txt should be created. Appropriate operations must be implemented inside the Author struct, and properly use the Data struct to compare author ages.
Finally, utilize the Library struct to store the lists of authors and books, and contain the functionality that the librarian requires.
You must use both member functions and non-member functions. One of the Book or Author sorting mechanisms must be implemented via member function (you can choose which) and the other with a non-member function.
You must use constructors to initialize all data variables in structs with default values at the beginning of the program. Use of global variables will incur a deduction of 10 points from your total points.
Assg1 code
#include <iostream> #include <fstream> #include <iomanip> #include <string> #include <stdlib.h> using namespace std; struct Date { int month; int day; int year; }; struct Author { string name; Date birth; string notable_work; string nationality; }; struct Book { string title; string author; int release_year; string language; string genre; }; void Menu(); void ReadBooks(ifstream& inFile, Book *books, int nun_books); void ReadAuthors(ifstream& inFile, Author *authors, int num_authors); void PrintBooks(Book *books, int size); void PrintAuthors(Author *authors, int size); void FindMatches(Book *books, Author *authors, int book_size, int author_size); string RemoveUnderScore(string word); int main() { Menu(); return 0; } void Menu() { ifstream inAuthors; ifstream inBooks; int input; redo: cout<<endl<<endl; cout<<setw(20)<<"1. Print Books"<<endl; cout<<setw(22)<<"2. Print Authors"<<endl; cout<<setw(22)<<"3. Print Matches"<<endl; cout<<setw(18)<<"4. Terminate"<<endl; cout<<setw(20)<<"Enter Selection:"; cin>>input; if(cin.fail() == true) { system("CLS"); cout<<"Invalid Input"<<endl; cin.clear(); cin.ignore(10000, '\n'); goto redo; } else if(input == 1) { system("CLS"); inBooks.open("Books.txt"); int num_books; inBooks>>num_books; Book books[num_books]; ReadBooks(inBooks, books, num_books); PrintBooks(books, num_books); inBooks.close(); } else if(input == 2) { system("CLS"); inAuthors.open("Authors.txt"); int num_authors; inAuthors>>num_authors; Author authors[num_authors]; ReadAuthors(inAuthors, authors, num_authors); PrintAuthors(authors, num_authors); inAuthors.close(); } else if(input == 3) { system("CLS"); inAuthors.open("Authors.txt"); inBooks.open("Books.txt"); int num_authors; inAuthors>>num_authors; Author authors[num_authors]; ReadAuthors(inAuthors, authors, num_authors); int num_books; inBooks>>num_books; Book books[num_books]; ReadBooks(inBooks, books, num_books); FindMatches(books, authors, num_books, num_authors); inBooks.close(); inAuthors.close(); } else goto end; goto redo; end: cout<<endl<<endl; cout<<"Termination"<<endl; } void ReadBooks(ifstream& inFile, Book *books, int num_books) { for(int i=0; i<num_books; i++) { inFile>>books[i].title; books[i].title= RemoveUnderScore(books[i].title); inFile>>books[i].author; books[i].author = RemoveUnderScore(books[i].author); inFile>>books[i].release_year; inFile>>books[i].language; inFile>>books[i].genre; } } void PrintBooks(Book *books, int size) { cout<<setw(60)<<"Books"<<endl<<endl; cout<<setw(10)<<"Title"<<setw(47)<<"Author"<<setw(28)<<"Release Year"<<setw(25)<<"Language"<<setw(30)<<"Genre"<<endl<<endl; for(int i=0; i< size; i++) { cout<<setw(40)<<left<<books[i].title<<setw(20)<<right<<books[i].author<<right<<setw(20)<<books[i].release_year<<setw(30)<<books[i].language<<setw(35)<<books[i].genre<<endl; } } void ReadAuthors(ifstream& inFile, Author *authors, int num_authors) { Date temp_date; string b_date; string temp; for(int i=0; i<num_authors; i++) { inFile>>authors[i].name; authors[i].name = RemoveUnderScore(authors[i].name); inFile>>b_date; temp= b_date.substr(0,2); authors[i].birth.month=atoi(temp.c_str()); temp= b_date.substr(3,2); authors[i].birth.day=atoi(temp.c_str()); temp = b_date.substr(6,4); authors[i].birth.year=atoi(temp.c_str()); authors[i].nationality = "unknown"; inFile>>authors[i].notable_work; authors[i].notable_work = RemoveUnderScore(authors[i].notable_work); } } void PrintAuthors(Author *authors, int size) { cout<<setw(60)<<"Authors"<<endl<<endl; cout<<setw(10)<<"Name"<<setw(30)<<"Date of Birth"<<setw(30)<<"Notable Work"<<endl<<endl; for(int i=0; i< size; i++) { cout<<setw(20)<<left<<authors[i].name<<right<<setw(10)<<authors[i].birth.month<<"/"<<authors[i].birth.year<<right<<setw(35)<<authors[i].notable_work<<endl; } } void FindMatches(Book *books, Author *authors, int book_size, int author_size) { cout<<setw(60)<<"Authors Matched"<<endl<<endl; cout<<setw(15)<<"Name"<<setw(30)<<"Nationality"<<setw(30)<<"Book"<<endl<<endl; for(int i=0; i<book_size; i++) for(int j=0; j<author_size; j++) { if(authors[j].name == books[i].author) { authors[j].nationality = books[i].language; cout<<setw(20)<<authors[j].name<<setw(25)<<authors[j].nationality<<setw(40)<<books[i].title<<endl; } } } string RemoveUnderScore(string word) { for(int i=0; i<word.length(); i++) { if(word[i]=='_') word[i]=' '; } return word; }
Books.txt
11 The_Name_of_the_Rose Umberto_Eco 1983 Italian Mystery 12000 Norwegian_Wood Haruki_Murakami 2000 Japanese Fiction 6000 The_Name_of_the_Wind Patrick_Rothfuss 2007 English Fantasy 4920 The_Girl_with_the_Dragon_Tattoo Stieg_Larsson 2005 Swedish Thriller 7049 The_Brothers_Karamazov Fyodor_Dostoevsky 1880 Russian Fiction 6948 Demonic_Males Richard_Wrangham 1997 English Non-Fiction 1209 War_and_Peace Leo_Tolstoy 1899 Russian Historical-Fiction 5890 Baudolino Umberto_Eco 2000 Italian Historical-Fiction 2134 The_Lies_of_Locke_Lamora Scott_Lynch 2006 English Fantasy 6676 The_Last_of_the_Wine Mary_Renault 1956 English Historical-Fiction 579 Brave_New_World Aldous_Huxley 1932 English Science-Fiction 4677
Authors.txt
7 Haruki_Murakami 01/12/1949 A_Wild_Sheep_Chase Leo_Tolstoy 09/09/1828 War_and_Peace Fyodor_Dostoevsky 11/11/1821 Crime_and_Punishment Nikos_Kazantzakis 02/18/1883 The_Last_Temptation Charles_Bukowski 08/16/1929 Post_Office George_Orwell 06/25/1903 1984 John_Milton 12/09/1608 Paradise_Lost
----------------------------------------------------------------------
Program
//Header files
#include<iostream>
#include<string>
#include<fstream>
#include<iomanip>
#include<ctime>
using namespace std;
//Book structure
struct Book {
string BookTitle;
string AuthorName;
int publicationYearInEnglish;
string OriginalLanguage;
string BookGenre;
int soldRate;
};
//Author structure
struct Author {
string AuthorName;
string DateOfBirth;
string NotableBook;
};
//Function prototypes
int readBooks(Book*,ifstream&);
int readAuthor(Author*, ifstream&);
int menu();
void printBooks();
void printAuthors();
void getBook(Book*,int);
string removeUnderScore(string);
void sortByPopularity(Book*, int);
void sortByAge(Author*, int);
//Main method
int main()
{
//File read object
ifstream in;
//Books.txt path
in.open("C:/Users/deept/Desktop/Books.txt");
//Book structure array
Book book[50];
//Read books details from Books.txt
int book_count=readBooks(book,in);
//Close file
in.close();
//Path for Author.txt
in.open("C:/Users/deept/Desktop/Author.txt");
//Author struct array
Author author[50];
//Read author details from Author.txt
int author_count = readAuthor(author, in);
//close file
in.close();
//User option
int ch = menu();
//If not exit continue
while (ch != 4) {
switch (ch) {
//Print all book
details in the collection
case 1:
printBooks();
break;
//Print all
author's details in the collection
case 2:
printAuthors();
break;
//Get favourite
author book and nationality
case 3:
getBook(book,book_count);
break;
default:
break;
}
ch = menu();
}
//End
cout << "\nProgram Ending......" <<
endl;
return 0;
}
//Method to read all book details
//Store in book array
//Call sort method to sort by sold values
//return count of book details in the collection
int readBooks(Book* book, ifstream& in) {
int count = 0;
if (!in) {
cout << "File can't be open"
<< endl;
exit(0);
}
else {
in >> count;
int i = 0;
while (!in.eof()) {
in >>
book[i].BookTitle>> book[i].AuthorName>>
book[i].publicationYearInEnglish >> book[i].OriginalLanguage
>> book[i].BookGenre>>book[i].soldRate;
i++;
}
}
sortByPopularity(book, count);
return count;
}
//Method to read all author details
//Store in author array
//call sort author method by their age
//return count of author details in the collection
int readAuthor(Author* author, ifstream& in) {
int count = 0;
if (!in) {
cout << "File can't be open"
<< endl;
exit(0);
}
else {
in >> count;
int i = 0;
while (!in.eof()) {
in >>
author[i].AuthorName >> author[i].DateOfBirth >>
author[i].NotableBook;
i++;
}
}
sortByAge(author, count);
return count;
}
//Method to display user options
//take user option
//check error
//Return option
int menu() {
int ch;
cout << " WELCOME TO
MARY's COOLECTION" << endl;
cout << "1.Print Books\n2.Print
Authors\n3.Search Favourite Author Book\n4.Quit" <<
endl;
cout << "Enter your choice: ";
cin >> ch;
while (ch < 1 || ch>4) {
cout << "Wrong
choice!!!.Choice should be 1..4\nRe-enter." << endl;
cout << "Enter your choice:
";
cin >> ch;
}
return ch;
}
//Method to remove all underscores in the given string
//return removed string
string removeUnderScore(string str) {
for (int i = 0; i < str.length(); i++)
{
if (str[i] =='_')
str[i] = '
';
}
return str;
}
//Method to print all books details in the collection by popularity
order
void printBooks() {
ifstream
in("c:/Users/deept/Desktop/OrderedBooks.txt");
if (!in) {
cout << "File can't be open "
<< endl;
exit(0);
}
else {
int i = 0;
string title, name,lang,gnr;
int yr;
cout << fixed;
cout << "
Book
Title
Book Name Published
Year
Language Genre\n"
<< endl;
while (!in.eof()) {
in >>
title >>name>>yr>>lang >> gnr;
cout <<
setw(25) << removeUnderScore(title) << "\t" <<
setw(20) << removeUnderScore(name) << "\t" <<
setw(10) << yr << "\t" << setw(10) << lang
<< "\t" << setw(10) <<gnr << endl;
i++;
}
}
in.close();
}
//Method to print all author details in the collection by ordered
age
void printAuthors() {
ifstream
in("c:/Users/deept/Desktop/OrderedAuthor.txt");
if (!in) {
cout << "File can't be open "
<< endl;
exit(0);
}
else {
//int i = 0;
string name,book;
cout << fixed;
cout <<
" Author
Name
Notable Book\n" << endl;
while (!in.eof()) {
in >>
name>>book;
cout <<
setw(20) << removeUnderScore(name) << "\t\t" <<
removeUnderScore(book) << endl;
//i++;
}
}
in.close();
}
//Method to get favourite author book
//Search in book list to get nationality
void getBook(Book* book,int n) {
string name;
cout << "Enter the author name: ";
cin.ignore();
getline(cin,name);
for (int j = 0;
j < n; j++) {
if (removeUnderScore(book[j].AuthorName) ==
name) {
cout << "Author name: "
<< name << "\t\t" << "Book Name: " <<
removeUnderScore(book[j].BookTitle) << "\t\t" <<
"Nationality: " << book[j].OriginalLanguage <<
endl;
return;
}
}
cout << name << " is not present in the
collection!!" << endl;
}
void sortByPopularity(Book* book, int n) {
for (int i = 0; i < n; i++) {
for (int j = i +
1; j<n; j++)
{
if (book[i].soldRate<book[j].soldRate)
{
int temp =
book[i].soldRate;
string title =
book[i].BookTitle;
string name =
book[i].AuthorName;
int yr =
book[i].publicationYearInEnglish;
string lang =
book[i].OriginalLanguage;
string gnr =
book[i].BookGenre;
book[i].soldRate =
book[j].soldRate;
book[i].BookTitle =
book[j].BookTitle;
book[i].AuthorName =
book[j].AuthorName;
book[i].publicationYearInEnglish =
book[j].publicationYearInEnglish;
book[i].OriginalLanguage =
book[j].OriginalLanguage;
book[i].BookGenre =
book[j].BookGenre;
book[j].soldRate =
temp;
book[j].BookTitle =
title;
book[j].AuthorName =
name;
book[j].publicationYearInEnglish = yr;
book[j].BookGenre =
gnr;
book[j].OriginalLanguage =
lang;
}
}
}
ofstream
out("c:/Users/deept/Desktop/OrderedBooks.txt");
if (!out) {
cout << "File can't be open "
<< endl;
exit(0);
}
else {
for (int i =0; i < n; i++)
{
out <<
book[i].BookTitle << " " << book[i].AuthorName <<
" "<< book[i].publicationYearInEnglish << " "<<
book[i].OriginalLanguage << " "<< book[i].BookGenre
<< endl;
}
}
out.close();
}
void sortByAge(Author* author, int n) {
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++)
{
if
(author[i].DateOfBirth < author[j].DateOfBirth ){
string name = author[i].AuthorName;
string dob = author[i].DateOfBirth;
string book = author[i].NotableBook;
author[i].AuthorName =
author[j].AuthorName;
author[i].DateOfBirth =
author[j].DateOfBirth;
author[i].NotableBook =
author[j].NotableBook;
author[j].AuthorName = name;
author[j].DateOfBirth = dob;
author[j].NotableBook = book;
}
}
}
ofstream
out("c:/Users/deept/Desktop/OrderedAuthor.txt");
if (!out) {
cout << "File can't be open "
<< endl;
exit(0);
}
else {
for (int i = 0; i < n; i++)
{
out
<<author[i].AuthorName<< " " <<
author[i].NotableBook<< endl;
}
}
out.close();
}
--------------------------------------------------------------------------------------
Output
WELCOME TO MARY's COOLECTION
1.Print Books
2.Print Authors
3.Search Favourite Author Book
4.Quit
Enter your choice: 1
Book
Title
Book Name Published
Year
Language Genre
The Name of the
Rose
Umberto Eco
1983
Italian
Mystery
The Girl with the Dragon
Tattoo Stieg
Larsson
2005
Swedish Thriller
The Brothers
Karamazov
Fyodor
Dostoevsky
1880
Russian
Fiction
The Lies of Locke
Lamora
Scott Lynch
2006
English
Fantasy
Norwegian
Wood
Haruki
Murakami
2000
Japanese
Fiction
War and
Peace
Leo Tolstoy
1899
Russian Historical-Fiction
The Name of the
Wind
Patrick
Rothfuss
2007
English
Fantasy
Brave New
World
Aldous Huxley
1932
English Science-Fiction
Baudolino
Umberto Eco
2000
Italian Historical-Fiction
Demonic
Males
Richard
Wrangham
1997
English Non-Fiction
The Last of the
Wine
Mary Renault
1956
English Historical-Fiction
The Last of the
Wine
Mary Renault
1956
English Historical-Fiction
WELCOME TO MARY's COOLECTION
1.Print Books
2.Print Authors
3.Search Favourite Author Book
4.Quit
Enter your choice: 2
Author
Name
Notable Book
John
Milton
Paradise Lost
Fyodor
Dostoevsky
Crime and Punishment
Leo
Tolstoy
War and Peace
Charles
Bukowski
Post Office
George
Orwell
1984
Nikos
Kazantzakis
The Last Temptation
Haruki
Murakami
A Wild Sheep Chase
Haruki
Murakami
A Wild Sheep Chase
WELCOME TO MARY's COOLECTION
1.Print Books
2.Print Authors
3.Search Favourite Author Book
4.Quit
Enter your choice: 3
Enter the author name: Leo Tolstoy
Author name: Leo
Tolstoy
Book Name: War and
Peace
Nationality: Russian
WELCOME TO MARY's COOLECTION
1.Print Books
2.Print Authors
3.Search Favourite Author Book
4.Quit
Enter your choice: 4
Program Ending......
Press any key to continue . . .
Screenshot
Objectives: The main objective of this assignment is checking students’ ability to implement membership functions. After...
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,...
Can anyone help me with my C++ assignment on structs, arrays and bubblesort? I can't seem to get my code to work. The output should have the AVEPPG from highest to lowest (sorted by bubbesort). The output of my code is messed up. Please help me, thanks. Here's the input.txt: Mary 15 10.5 Joseph 32 6.2 Jack 72 8.1 Vince 83 4.2 Elizabeth 41 7.5 The output should be: NAME UNIFORM# AVEPPG Mary 15 10.50 Jack 72 8.10 Elizabeth 41 7.50 Joseph 32 6.20 Vince 83 4.20 My Code: #include <iostream>...
I need to add something to this C++ program.Additionally I want it to remove 10 words from the printing list (Ancient,Europe,Asia,America,North,South,West ,East,Arctica,Greenland) #include <iostream> #include <map> #include <string> #include <cctype> #include <fstream> #include <iomanip> using namespace std; void addWord(map<std::string,int> &words,string s); void readFile(string infile,map<std::string,int> &words); void display(map<std::string,int> words); int main() { // Declaring variables std::map<std::string,int> words; //defines an input stream for the data file ifstream dataIn; string infile; cout<<"Please enter a File Name :"; cin>>infile; readFile(infile,words);...
I need to make a few changes to this C++ program,first of all it should read the file from the computer without asking the user for the name of it.The name of the file is MichaelJordan.dat, second of all it should print ,3 highest frequencies are: 3 words that occure the most.everything else is good. #include <iostream> #include <map> #include <string> #include <cctype> #include <fstream> #include <iomanip> using namespace std; void addWord(map<std::string,int> &words,string s); void readFile(string infile,map<std::string,int> &words); void display(map<std::string,int>...
I want to change this code and need help. I want the code to not use parallel arrays, but instead use one array of struct containing the data elements, String for first name, String for last name,Array of integers for five (5) test scores, Character for grade. If you have any idea help would be great. #include #include #include #include using namespace std; const int NUMBER_OF_ROWS = 10; //number of students const int NUMBER_OF_COLUMNS = 5; //number of scores void...
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!";...
MUST BE WRITTEN IN C++ Objective: Learn how to define structures and classes, create and access a vector of structures, use sort with different compare functions. Assignment: Your program will use the same input file, but you will print the whole book information, not just the title. And, most importantly, this time you will take an object oriented approach to this problem. Detailed specifications: Define a class named Collection, in which you will define a structure that can hold book...
MUST BE WRITTEN IN C++ Objective: Learn how to define structures and classes, create and access a vector of structures, use sort with different compare functions. Assignment: Your program will use the same input file, but you will print the whole book information, not just the title. And, most importantly, this time you will take an object oriented approach to this problem. Detailed specifications: Define a class named Collection, in which you will define a structure that can hold book...
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...
How can I make this compatible with older C++ compilers that DO NOT make use of stoi and to_string? //Booking system #include <iostream> #include <iomanip> #include <string> using namespace std; string welcome(); void print_seats(string flight[]); void populate_seats(); bool validate_flight(string a); bool validate_seat(string a, int b); bool validate_book(string a); void print_ticket(string passenger[], int i); string flights [5][52]; string passengers [4][250]; int main(){ string seat, flight, book = "y"; int int_flight, p = 0, j = 0; int seat_number,...