Here\'s the instructions: Assignment : Your instructor maintains a database of t
ID: 3537205 • Letter: H
Question
Here's the instructions:
Assignment:
Your instructor maintains a database of technical books in his collection. You will find a copy of this database in the file: cs161_Assignment7_library.txt which is part of this assignment. The format of this file is such that every book occupies two lines. The first line contains the title and the second line contains the author or authors.
Step (1)
Your first task is to write a program which reads this file into two parallel arrays in memory. One array contains the titles, and the other array contains the authors. The arrays are %u2018parallel%u2019 in the sense that the nth element of the author array contains the authors associated with the nth element of the title array. Although the data in the test file describes only a few books you should dimension your arrays for an arbitrary number of books in the live data file (which may be different from the test file). The size of the arrays should be defined in your program by the declaration
const int ARRAY_SIZE = 1000
For the purpose of this assignment (so as not to make it too complicated) you should declare your two arrays at file scope (i.e. outside any function and before the function main()). The declarations should be as follows:
string bookTitle [ARRAY_SIZE];
string bookAuthor [ARRAY_SIZE];
Write a function whose signature is as follows:
int loadData (string pathname)
The function loadData should open the data file located in the path on your hard disk specified by pathname. If it cannot open the file for some reason, it should return a value of -1 as an error result. Otherwise it should read the data in the file into the two parallel arrays, and return the number of book records that it read.
Now write a function whose signature is as follows:
void showAll (int count)
The parameter count provides the number of books actually stored in the database (i.e. the value returned by the function loadData()). showAll() should read the designated number of records in the parallel arrays and display it on the screen in the order it appears in the database, in single column format. In this display format the author or authors appear in parentheses following the title and there is one book record per line of output. For example, using the data in the sample file, the output would look like this:
Objects First with Java (Barnes and Kolling)
Game Development Essentials (Novak)
The Game Maker's Apprentice (Overmars)
C++ Programming: From Problem Analysis%u2026 (Malik)
%u2026
%u2026
Audio for Games (Brandon)
Now test your work so far, by writing a main() function to prompt the user for the path to the data file, open and read the file to the parallel arrays using the function loadData() (providing error messages and recovery as appropriate if the file doesn%u2019t exist). Then display the contents of the file using showAll().
Step (2)
Extend your program into a menu driven application to query the database. When the program starts it should ask the user for the path to the data file, and read the data into the parallel arrays, as in step 1.
Then it should enter a loop such that in each iteration of the loop it prompts the user for what they want to do. The actions are as follows depending on how the user responds to the prompt. See the sample dialog below to see how this works in practice.
Note: In the example below, the user%u2019s input is in BOLD. The program output is not.
Welcome to <YourNameHere>%u2019s Library Database.
Please enter the name of the backup file: C:library.txt
14 records loaded successfully.
Enter Q to (Q)uit, Search (A)uthor, Search (T)itle, (S)how All: A
Author%u2019s Name: Malik
C++ Programming: From Problem Analysis... (Malik)
C++ Programming: Program Design Including... (D. S. Malik)
2 records found.
Enter Q to (Q)uit, Search (A)uthor, Search (T)itle, (S)how All: A
Author%u2019s Name: Goble
0 records found.
Enter Q to (Q)uit, Search (A)uthor, Search (T)itle, (S)how All: T
Title: Game
Game Development Essentials (Novak)
The Game Maker's Apprentice (Overmars)
Game Character Development with Maya (Ward)
Developing Games in Java (Brackeen)
Audio for Games (Brandon)
5 records found.
Enter Q to (Q)uit, Search (A)uthor, Search (T)itle, (S)how All: Q
Design Considerations:
1) Please display your name and not <YourNameHere> in the welcome message.
2) It is an essential part of your grade that you design your application according to the considerations described above. For example, you must use functions that have the specified signatures, and arrays that have the specified declarations. They are as follows:
// These declarations should be at file scope
const int ARRAY_SIZE = 1000;
string bookTitle [ARRAY_SIZE];
string bookAuthor [ARRAY_SIZE];
// Function prototypes
int loadData(string pathname);
void showAll(int count);
void showBooksByAuthor (int count, string name);
void showBooksByTitle (int count, string title);
Explanation / Answer
please rate - thanks
I would have done more, but don't have a sample of the input file
your problem is that int ShowBooksByAuthor and
int ShowBooksByTitle
need to be value returning and not void
I added function stubs for them, so that it would compile cleanly
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
const int ARRAY_SIZE = 1000;
string bookTitle[ARRAY_SIZE];
string bookAuthor[ARRAY_SIZE];
int LoadData(string pathname);
void ShowAll(int count);
int ShowBooksByAuthor(int count, string name);
int ShowBooksByTitle(int count, string title);
int main()
{
int count = 0;
char selector = 'q', yesNoAnswer = 'n';
string name;
string title;
cout << "Welcome to Feilong's Library Database. ";
do
{
cout << " (L)oad File, (S)how All, (A)uthor,(T)itle, (Q)uit: ";
cin >> selector;
selector = toupper(selector);
switch(selector)
{
case 'L':
if (count > 0)
{
cout << "Are you sure you want to delete previous data? (Y/N): ";
cin >> yesNoAnswer;
if (yesNoAnswer == 'Y' || yesNoAnswer == 'y')
count = 0;
else
break;
}
count = LoadData(name);
if (count == -1)
cout << "Cannot open the input file! ";
else
cout << count << "count(s) found. ";
break;
case 'S':
if (count <= 0)
cout << "No counts found! ";
else
ShowAll(count);
break;
case 'A':
cout << "bookAuthor: ";
cin >> name;
if (count <= 0)
cout << "No counts found! ";
else
cout<<ShowBooksByAuthor(count, name)
<< "count(s) found. ";
break;
case 'T':
cout << "bookTitle: ";
cin >> title;
if (count <= 0)
cout << "No counts found! ";
else
cout << ShowBooksByTitle(count, title)
<< "count(s) found. ";
break;
}
}
while (selector != 'q' && selector != 'Q');
return 0;
}
int LoadData(string pathname)
{
int count;
ifstream infile;
infile.open("\Users\Owner\Desktop\pathname.txt");
if (!infile)
{
cout << "Cannot open backup file" << endl;
return -1;
}
else
{
cout << "File opened" << endl;
}
while (!infile.eof())
{
getline(infile, bookTitle[count]);
getline(infile, bookAuthor[count]);
count++;
}
return count;
}
void ShowAll(int count)
{
int i;
for (i = 0; i < count; i++)
{
cout << bookTitle[i] << " " << "(" << bookAuthor[i] << ")" << endl;
}
}
int ShowBooksByAuthor(int count, string name)
{int kount=0;
return kount;
}
int ShowBooksByTitle(int count, string title)
{int kount=0;
return kount;
}