This program will hold information about 3 books and allow the user to view or m
ID: 3570783 • Letter: T
Question
This program will hold information about 3 books and allow the user to view or modify this information. Declare a struct Book with 2 string fields to hold the title and the author. In your main function, declare an array with three elements in which each element is a pointer to a struct Book. Call a function with the following prototype: void Initalize(struct Book * [], int size); The function will dynamically allocate three struct Book objects and the address of the first struct Book object will be assigned to the first array element, the address of the second struct Book object will be assigned to the second array element, and so forth. The default value for the title and author fields should be
Explanation / Answer
I wrote it in C++, though I can redo it in pure C if you want. If you have any questions please leave a comment! The code can also be found here: http://pastebin.com/AHijnFP7.
/* Books.cpp */
#include <iostream>
#include <string>
using namespace std;
#define NUM_BOOKS 3
struct Book {
string title;
string author;
};
enum MenuCase:int {VIEW = 1, MODIFY = 2, QUIT = 3};
void initialize(struct Book *books[], size_t size);
bool getChoice(int &choice, int min, int max);
int main() {
// Initialze an array of Book pointers of size NUM_BOOKS
struct Book *books[NUM_BOOKS];
initialize(books, NUM_BOOKS);
// Main menu loop
int answer = 0;
while (answer != QUIT) {
// Get a menu choice from the user
cout << "== MAIN MENU == "
<< "1) View book information "
<< "2) Modify book information "
<< "3) Quit ";
do {
cout << "Please enter the number corresponding to the desired action: ";
} while (!getChoice(answer, VIEW, QUIT));
switch (answer) {
case VIEW:
// Print all book information
for (size_t i = 0; i < NUM_BOOKS; ++i) {
cout << " Book " << (i + 1) << ": "
<< " Title: " << books[i]->title << " "
<< " Author: " << books[i]->author;
}
cout << " ";
break;
case MODIFY:{
// Get the book number to modify
int bookN = 0;
cout << " There are " << NUM_BOOKS << " total books. "
<< "Which book would you like to modify? (enter number from 1 to " << NUM_BOOKS << ") ";
while (!getChoice(bookN, 1, NUM_BOOKS))
;
// Get the new book information
string newTitle, newAuthor;
cout << "Please enter the title of book " << bookN << ": ";
getline(cin, newTitle);
cout << "Please enter the author of book " << bookN << ": ";
getline(cin, newAuthor);
// Modify the book information
books[bookN - 1]->title = newTitle;
books[bookN - 1]->author = newAuthor;
cout << "Book " << bookN << " has been successfully modified. ";
break;
}
case QUIT:
cout << " Goodbye! ";
break;
default:
break;
}
}
// Free the memory allocated for the book array
for (size_t i = 0; i < NUM_BOOKS; ++i) {
delete books[i];
}
return 0;
}
// Initialize the array of Book pointers
void initialize(struct Book *books[], size_t size) {
for (size_t i = 0; i < size; ++i) {
books[i] = new Book;
books[i]->author = "NONE";
books[i]->title = "NONE";
}
}
// Get an integer choice in the range (min, max). Choice is returned by reference and
// will not be modified if an invalid choice is entered.
bool getChoice(int& choice, int min, int max) {
int t = min;
if (!(cin >> t)) { //non-number entered
cout << " Please enter a number from " << min << " to " << max << ". ";
cin.clear();
cin.ignore(numeric_limits<int>::max(), ' ');
return false;
} else if (t < min || t > max) { //number out of range
cout << " Please enter a number from " << min << " to " << max << ". ";
return false;
} else { //valid number
cin.ignore(numeric_limits<int>::max(), ' ');
choice = t;
return true;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.