Hi again.
In one of my programs, I would like to put in a search type feature. I would like it, so if the user enters a book name, it returns the call number of the book. I would also like it ti display a message if the library doesn't have this book.
The way I have it is problematic because it looks like this:

int searchEngine()
{
string name;
string number;
cout << "Enter the name of a book: \n";
cin >> name;
[INDENT]if ( name == "BillyBob")
{
[INDENT]number = "CAL 123.45";[/INDENT]
}
//Then LOTS more of the if statements
//followed by...
else
{
[INDENT]cout << "The library doesn't own this book!\n";[/INDENT]
}
[/INDENT]
}

That would make my code huge, and the task would be tedious. Is there any way to get all of this code shortened or off site into a .h file?

As you probably already guessed, you don't do that with a bunch of if statements.

I guess you already have a list of books with their titles and call numbers, maybe other information too. In that case, all you have to do is code a loop that searches the list of books. If you have them stored on disk in a file then use fstream to read each book and compare the book name with user input.

Yeah, I've used ofstream before but never the input. Also, would the text file just have to look like:
BillyBob = CAL 123.45
or something different. Any links/tutorials would be greatly appreciated

you could setup a little database which would just be a file and have the program open up the file and search it for the name, ahh what the heck ill show you what i mean, heres the code.

first make a file in the local directory of your program and call it "books.database"
copy and paste this code into the file and save it, than run the program with the code here that I made.

BillyBob CAL.0
JohnsBook CAL.1
Kreamer CAL.2
History CAL.3
Physics CAL.4
Math CAL.5
Biology CAL.6

now if you type in the search prompt Biology it will return and display CAL.6 as the books number, i dunno the format you want it in so you can play around with it and learn from there but i hope it helps you out.

now heres the code for the program itself.

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
bool BookFound = false;
string name;
string number;
string tempName;
cout << "Enter Book Name: ";
cin >> name;

ifstream LibraryDatabaseFile;
LibraryDatabaseFile.open("books.database");

if (!LibraryDatabaseFile.is_open())
{
cout << "\n";
cout << "error reading the library database";
}

while (LibraryDatabaseFile >> tempName)
{
if (tempName == name)
{
BookFound = true;
LibraryDatabaseFile >> number;
cout << "\n";
cout << "Book Number: ";
cout << number;
}
}
LibraryDatabaseFile.close();
if (BookFound == false)
{
cout << "\n";
cout << "The library doesn't own this book\n";
}
}

You can design the text file any way you want to, unless you were told otherwise. What you posted will work. Another common method is to sepate fields with a comma, assuming a comma does not occur in the book name or other fields.

BillyBob, CAL 123.45

And you use ifstream for input. ofstream is only for output.

[edit]What ^^^ said works too :) [/edit]

Thanks guys! That worked perfectly! And...If there was other informatin that needed to be displayed I'd just need to put:

LibraryDatabaseFile >> author;
cout << "\n";
cout << "Book Author: ";
cout << author;

or something like that?

yeah just make sure you structure your database file accordingly to how your program reads it.

This article has been dead for over six months. Start a new discussion instead.