book catalog

Hi,

I've been working on this problem for a while and am unable to get to the right solution. Can someone help me create a Book Catalog that would contain a collection of books (maximum of 500), with following fields of information: book code (ISBN number), author’s last name, author’s first name, book title, year of publication and price. The book catalog should use a file for storing the information on books.The information in the file is sorted alphabetically by title of the book, with a tab separating each piece of information about the book, with one book’s information per line.
I have to use C++ Arrays and Structs (cannot use Vectors or other
collection classes available thru libraries). The program should have the following features:
1. The program should allow the user to add a new book to the catalog. Use this
feature to initially create the book catalog. The program should ask for the
required information and then add the book to the catalog, only if there is no other
book with the same code in the catalog.
2. The program should allow user to find a book in the catalog, given one of the
following: author first name or last name or the book code. If the book is found,
all the information about the book should be displayed. If the book is not found,
an appropriate message should be displayed.
3. The program should allow you to delete an existing book from the catalog.
4. The program should have an option to display all the current books in the
catalog. This should display the books sorted on their titles (in alphabetical
order).
5. The program should have a menu driven interface to allow users to select one of
the options to perform the above tasks. The program should display appropriate
messages to the user for each operation, successful or not.
6. When exiting, the program should save the book catalog to file, taking in to
account any of the above changes, for future reuse.

My attempt to solve the above problem is as follows (doesn't work):
# include <iostream>
# include <fstream>
# include <cstdlib>
# include <cmath>
# include <string>
using namespace std;

const int N_BOOKS= 2;
//#define inFile "booklistIn.txt" // directory name for inFile
#define outFile "booklistOut.txt" // directory name for outFile

struct bookList
{
string bookCode;
string authorFName;
string authorLName;
string bookTitle;
int yearPub;
float price;
};

//void printBook(bookList book);
//void copyBookList(ifstream&, ofstream&);


int main()
{
int i;
bookList aBook[N_BOOKS];

// ifstream fin; // fin is an input stream
ofstream fout; // fout is an output stream

//open output stream, exit on any error

fout.open(outFile); //connects outs to file outFile
if (fout.fail())
{
cerr << "*** ERROR: cannot open " << outFile << " for output." << endl;
return EXIT_FAILURE; // failure return

}



for (int i=0; i<N_BOOKS; i++)
{
cout <<"Enter book code: ";
getline (cin,aBook[i].bookCode);
fout << aBook[i].bookCode;

cout << "Enter book title: ";
getline (cin,aBook[i].bookTitle);
fout << aBook[i].bookTitle;

cout << "Enter book author Firstname: ";
getline (cin,aBook[i].authorFName);
fout << aBook[i].authorFName;

cout << "Enter book author Lastname: ";
getline (cin,aBook[i].authorLName);
fout << aBook[i].authorLName;

cout << "Enter published year book: ";
cin >> aBook[i].yearPub;
fout << aBook[i].yearPub;

cout << "Enter book price: ";
cin >> aBook[i].price;
fout << aBook[i].price;
}

//for (int i=0; i<N_BOOKS; i++)
//{printBook(aBook[i]);}
fout.close();
system ("pause");
return 0;
}


thanks in advance,
upad
so what problems are you having?
What output are you getting?
Where is the output inconsistent with your expectations?
Are you getting compiler errors...or does it compile without warnings?
Please enclose your code in code tabs so that we can read it easier and have line numbers
to which we can refer.
all of your code must be within the scope of a function. everything you have that appears after the end of main() is invalid as it just loose code floating around. This will never compile.

Edit: Sorry my bad. Please use the icon to the right that looks like <> to post code in order to keep your code formatting. It had looked as if the } from if (fout.fail()){ had been the end bracket to main(). If you are not already you should be indenting your brackets by at leach 3 spaces to 1 tab for easier code readabilty.

Last edited on
Topic archived. No new replies allowed.