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
ANSWER:
Objectives: The main objective of this assignment is checking students’ ability to implement membership functions. After completing this assignment, students will be able to:
PROGRAM:
#include <iostream>
#include <fstream>
#include <iomanip>
#include <algorithm>
using namespace std;
struct Date{
int day;
int month;
int year;
};
struct Dimension{
double width;
double height;
double depth;
};
struct Furniture{
string name;
string color;
int inventory;
Date dateCreated;
double cost;
bool operator<(const Furniture &other) const
{
return cost < other.cost;
}
/*bool operator()(const Furniture& lhs, const Furniture& rhs) const { lhs.cost < rhs.cost; }
*/
};
struct Art{
string name;
double cost;
int inventory;
Date dateCreated;
Dimension artSize;
bool operator<(const Art &other) const
{
return cost < other.cost;
}
};
struct Fabric{
string name;
string color;
int inventory;
double cost;
bool operator<(const Fabric &other) const
{
return cost < other.cost;
}
};
struct Book{
string name;
bool hardCover;
int numPages;
double cost;
int inventory;
Date datePublished;
bool operator<(const Book &other) const
{
return cost < other.cost;
}
};
/**
These four functions are mandatory as instructed in the Assignment
**/
void getFurnitureData(ifstream& inFile);
void getArtData(ifstream& inFile);
void getFabricData(ifstream& inFile);
void getBookData(ifstream& inFile);
/**
Optional Helper Functions
**/
void displayFurniture(Furniture furniture[], int totalFurniture);
void displayArt(Art art[], int totalArt);
void displayFabric(Fabric fabric[], int totalFabric);
void displayBook(Book book[], int totalBook);
void printHorizontalLine( int width, char border_char);
void printHeading( string title, int width );
int main()
{
cout << "This is Assignment 1 (Structs) - Home Decoration Store" << endl;
/**
Declaring the input streams for each file
**/
ifstream inFileFurniture;
ifstream inFileArt;
ifstream inFileFabric;
ifstream inFileBook;
/**
Opening the files. You can either hardcode the name of the files or ask the user to give the names
**/
inFileFurniture.open("Furniture.txt");
inFileArt.open("Art.txt");
//inFileFabric.open("Fabric.txt");
//inFileBook.open("Book.txt");
/**
If the any of the file cannot be opened then the program terminates displaying
the error message
**/
if (!inFileFurniture)
{
cout << "The Furniture input file does not exist. Program terminates!" << endl;
//return 1;
}
if (!inFileArt)
{
cout << "The Art input file does not exist. Program terminates!" << endl;
return 1;
}
/*
if (!inFileFabric)
{
cout << "The Fabric input file does not exist. Program terminates!" << endl;
return 1;
}
if (!inFileBook)
{
cout << "The Book input file does not exist. Program terminates!" << endl;
return 1;
}
*/
/**
Display the prompt and do the requested action. Keep repeating the prompt until exit.
If the number entered is not an option, just repeat the prompt.
**/
int chosenOption;
do{
cout << "\n\n\nSelect which option you would like to see \n"
<< "1. Print all Furniture \n"
<< "2. Print all Art \n"
<< "3. Print all Fabric \n"
<< "4. Print all Book \n"
<< "5. Exit \n"
<< "\n"
<< "Enter Option (1-5): ";
cin >> chosenOption;
/**
Do the correct action according to the chosenOption
**/
switch(chosenOption)
{
case 1:
/**
Furniture
Function call to read data from input file. That function then calls a print Function
**/
getFurnitureData(inFileFurniture);
break;
case 2:
/**
Art
Function call to read data from input file. That function then calls a print Function
**/
getArtData(inFileArt);
break;
case 3:
/**
Fabric
Function call to read data from input file. That function then calls a print Function
**/
getFabricData(inFileFabric);
break;
case 4:
/**
Book
Function call to read data from input file. That function then calls a print Function
**/
getBookData(inFileBook);
break;
default:
break;
}
}while(chosenOption != 5);
return 0;
}
void printHorizontalLine( int width, char border_char){
cout.fill( border_char );
cout << setw( width ) << border_char << "\n";
cout.fill(' ');
}
void printHeading( string title, int width ){
//Declare Variables
int magic_width = 0;
magic_width = (width/2) - (title.length()/2) + title.length();
cout << "\n";
cout << left << setfill('=') << setw( width ) << "=" << "\n";
cout << right << setfill(' ') << setw( magic_width ) << title << "\n";
cout << right << setfill('=') << setw( width ) << "=" << endl;
//reset count
cout.clear();
cout.fill(' ');
//VOID functions do NOT return a value
}
/**
Using the input stream sent as parameter we are reading the content from the Furniture.txt and storing it in the Furniture struct array
**/
void getFurnitureData(ifstream& inFile){
int totalFurniture;
inFile >> totalFurniture;
Furniture furniture[totalFurniture];
char decimal;
for(int i = 0; i < totalFurniture; i++){
inFile >> furniture[i].name;
inFile >> furniture[i].color;
inFile >> furniture[i].inventory;
inFile >> furniture[i].dateCreated.month >> decimal >> furniture[i].dateCreated.day >> decimal >> furniture[i].dateCreated.year;
inFile >> furniture[i].cost;
}
inFile.close();
printHeading("Furniture", 60);
std::sort(furniture, furniture+totalFurniture);
displayFurniture(furniture,totalFurniture);
}
/**
Using the input stream sent as parameter we are reading the content from the fabric.txt and storing it in the fabric struct array
**/
void getFabricData(ifstream& inFile){
int totalFabric;
inFile >> totalFabric;
Fabric fabric[totalFabric];
for(int i = 0; i < totalFabric; i++){
inFile >> fabric[i].name;
inFile >> fabric[i].color;
inFile >> fabric[i].inventory;
inFile >> fabric[i].cost;
}
inFile.close();
printHeading("Fabric", 60);
std::sort(fabric, fabric+totalFabric);
displayFabric(fabric,totalFabric);
}
/**
Using the input stream sent as parameter we are reading the content from the Art.txt and storing it in the art struct array
**/
void getArtData(ifstream& inFile){
int totalArt;
inFile >> totalArt;
Art art[totalArt];
char decimal;
for(int i = 0; i < totalArt; i++){
inFile >> art[i].name;
inFile >> art[i].cost;
inFile >> art[i].inventory;
inFile >> art[i].dateCreated.month >> decimal >> art[i].dateCreated.day >> decimal >> art[i].dateCreated.year;
inFile >> art[i].artSize.width >> decimal >> art[i].artSize.height >> decimal >> art[i].artSize.depth;
}
inFile.close();
printHeading("Art", 60);
std::sort(art, art+totalArt);
displayArt(art,totalArt);
}
/**
Using the input stream sent as parameter we are reading the content from the Personnel.txt and storing it in the Book struct array
**/
void getBookData(ifstream& inFile){
int totalBook;
inFile >> totalBook;
Book book[totalBook];
char decimal;
for(int i = 0; i < totalBook; i++){
inFile >> book[i].name;
inFile >> book[i].hardCover;
inFile >> book[i].numPages;
inFile >> book[i].cost;
inFile >> book[i].inventory;
inFile >> book[i].datePublished.month >> decimal >> book[i].datePublished.day >> decimal >> book[i].datePublished.year;
}
inFile.close();
printHeading("Books", 60);
std::sort(book, book+totalBook);
displayBook(book,totalBook);
}
/**
Displaying the content from the Furniture struct array on the monitor
**/
void displayFurniture(Furniture furniture[], int totalFurniture){
cout << setw(10) << "Name" << " | "
<< "Color" << " | "
<< "Inventory" << " | "
<< "Date Created" << " | "
<< "Cost" << endl;
printHorizontalLine(80,'-');
for(int i = 0; i < totalFurniture; i++){
cout << left << setw(13) << furniture[i].name << " | "
<< right << setw(6) << setfill(' ') << furniture[i].color << " | "
<< right << setw(6) << setfill(' ') << furniture[i].inventory << " | "
<< right <<setw(2) << setfill('0') << furniture[i].dateCreated.month <<":" << setw(2) << setfill('0') << furniture[i].dateCreated.day <<":" << setw(2) << setfill('0') << furniture[i].dateCreated.year << " | "
<< right <<setw(9) << setfill(' ') << furniture[i].cost << " "
<< setfill(' ') << endl;
}
}
/**
Displaying the content from the Fabric struct array on the monitor
**/
void displayFabric(Fabric fabric[], int totalFabric){
cout << setw(15) << "Name" << " | "
<< "Color" << " | "
<< "Inventory" << " | "
<< "Cost" << endl;
printHorizontalLine(80,'-');
for(int i = 0; i < totalFabric; i++){
cout << left << setw(15) << setfill(' ') << fabric[i].name << " | "
<< right << setw(5) << setfill(' ') << fabric[i].color << " | "
<< right << setw(5) << setfill(' ') << fabric[i].inventory << " | "
<< right << setw(3) << setfill(' ') << fabric[i].cost << " "
<< setfill(' ') << endl;
}
}
/**
Displaying the content from the Art struct array on the monitor
**/
void displayArt(Art art[], int totalArt){
cout << setw(18) << "Name" << " | "
<< " Cost" << " | "
<< "Inventory" << " | "
<< "Date Created" << " | "
<< "Art Dimensions" << endl;
printHorizontalLine(80,'-');
for(int i = 0; i < totalArt; i++){
cout << left << setw(18) << art[i].name << " | "
<< right << setw(5) << art[i].cost << " | "
<< right << setw(4) << setfill(' ') << art[i].inventory << " | "
<< right << setw(2) << setfill('0') << art[i].dateCreated.month <<":" << setw(2) << setfill('0') << art[i].dateCreated.day <<":" << setw(2) << setfill('0') << art[i].dateCreated.year << " | "
<< right << setw(2) << setfill('0') << art[i].artSize.width <<":" << setw(2) << setfill('0') << art[i].artSize.height <<":" << setw(2) << setfill('0') << art[i].artSize.depth << " | "
<< setfill(' ') << endl;
}
}
/**
Displaying the content from the Personnel struct array on the monitor
**/
void displayBook(Book book[], int totalBook){
cout << setw(25) << "Name" << " | "
<< "Is Hardcover" << " | "
<< "Number Pages" << " | "
<< "Cost" << " | "
<< "Inventory" << " | "
<< "Date Published" << endl;
printHorizontalLine(80,'-');
for(int i = 0; i < totalBook; i++){
cout << left << setw(25) << book[i].name << " | "
<< right << setw(7) << setfill(' ') << book[i].hardCover << " | "
<< right << setw(9) << setfill(' ') << book[i].numPages << " | "
<< right << setw(9) << setfill(' ') << book[i].cost << " | "
<< right << setw(9) << setfill(' ') << book[i].inventory << " | "
<< right << setw(2) << setfill('0') << book[i].datePublished.month <<":" << setw(2) << setfill('0') << book[i].datePublished.day <<":" << setw(2) << setfill('0') << book[i].datePublished.year << " | "
<< setfill(' ') << endl;
}
}
OUTPUT
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...
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...