HI!. So Im good with everything except when I call the printone function in the findtitle function. Basically, I am sending a string str as a parameter to the findtitle funtion. Im trying to find this str string in a much larger string that Im getting from an input file. The input file looks like this by the way:
1984 George Orwell Science_Fiction Secker_and_Warburg 8_June_1949
1984 George Orwell Science_Fiction Secker_and_Warburg 8_June_1949
1984 George Orwell Science_Fiction Secker_and_Warburg 8_June_1949
1984 George Orwell Science_Fiction Secker_and_Warburg 8_June_1949
1984 George Orwell Science_Fiction Secker_and_Warburg 8_June_1949
Then if it finds it, the printone function is called and performs its objective.
If not, message "Not Found" appears. Problem is that when i do enter the title, in this case 1984, the output prints "Not Found" no matter what. This is my only question: Why does it keep printing "Not Found", when its supposed to find it based on my code and based on the str i enter. Here is the sample output:
There are 5 objects in total
How many objects do you plan to process? (max is 5): 1
1984 George Orwell Science_Fiction Secker_and_Warburg 8_June_1949
Enter the title of the book you are looking for:
1984
Not Found
Process returned 0 (0x0) execution time : 6.126 s
Press any key to continue.
In case anything is unclear, I have also posted the entire assignment on the bottom, though i dont think there is much of a need for you to look at it.
Any help is appreciated.
P.S the reason why there are five exactly the same lines is because Im currently just testing out the program. When i Know it works, i will make the input file more random.
My code:
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
using namespace std;
const int NUMBOOKS = 25;
class Authorname {
public:
string first;
string last;
};
class Publisher_info {
public:
string publisher;
string year;
};
class BookCollection {
public:
string booktitle;
Authorname authorname;
string category;
Publisher_info publisher;
};
void readdata (BookCollection book [], int &num);
void printall (BookCollection book [], int num);
void findtitle (BookCollection book [], int num, string str);
void printone (string);
BookCollection book [NUMBOOKS];
int main () {
int num;
cout << "There are 5 objects in total" << endl;
cout << "How many objects do you plan to process? (max is 5): ";
cin >> num;
cout << endl;
readdata(book, num);
printall(book, num);
string str;
cout << "Enter the title of the book you are looking for: " << endl;
cin >> str;
findtitle (book, num, str);
return 0;
}
void readdata (BookCollection book [], int &num)
{
ifstream infilebook ("inbook.txt");
if (!infilebook.is_open()) {
cout << "Error opening input file" << endl;
exit(1);
}
Write a complete C++ program, including good comments, to do the following:
The program will maintain a database of something you like to collect, such as CD’s, DVD’s or books.
Your program will create a class and define an array of objects of that class. The class must have at least
two fields, and at least one of the fields must be another nested class.
For example, for a BookCollection class, you might store the following information:
book title, author’s name (which is another class divided into first and last names), category (such as
fiction, non-fiction, mystery, etc.), and publisher info (another class divided into publisher and year).
Define the class above main() so that you can use it in all the functions. Declare the actual array of
objects in the main program.
The program will begin by reading in the data from a file. Call a function readdata sending it two
parameters: the array of objects and n as a reference parameter. The function will read in the value of n
and then information about n objects. For each item, read in a value for each of the fields in the class.
The data must be entered in the input file in the exact order of the members of the class. If any titles
have more than one word, use underscores to eliminate whitespace so that you can use infile>> to
read them. Otherwise, you can use getline() but you will need some calls to cin.ignore().
Next, the program will print all the data by calling a function printall, sending it two parameters, the
array of objects and n, the number of filled positions in the array. The function will print info for each
item in your collection on a new line (for example, title, author, category and publisher-info).
Next, the main program will read in a value from the keyboard and call a search function to find that
item in the array of items. You only need to write one search function, but you may write more if you
want to. Choose an appropriate name, such as findtitle, findauthor, findcategory, findpublisher, or
findyear.
The findxxx function will receive three parameters: the array of objects, n (number of filled positions in
the array), and the value to search for. The search function will first print the item to search for and then
search for the item. Each time the item is found, it will call printone, to print all of the information for
the object in the array that matches the search value. For example, if I have the function findyear and I
search for the year 2011, I would call printone to print the title, author, category and publisher info for
each book that was published in the year 2011. The only parameter sent to printone is the single
element in the array that matches the year, for example mybook[0]. If the search item is not found in
the array, the function should print “not found”.
DATA: Your array should allow for up to 25 items. To test the program, have a set with at least 5 items.
Make sure that at least 2 items contain the value you are searching for. For example, if you are
searching for the year 2011, be sure to have at least 2 books published in 2011.