code using structs and external files

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
Add "cin.ignore();" at the end of the loop to get rid of the extra newline in the buffer that "cin >> " makes.
so, should I add cin.ignore(); as follows??:

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

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

thanks again,
upad
You need to start by reading in your library from file into your array of books, aBook. And you'll need to save to the library file at the end.

You'll need to keep track of how many books are actually in the library with a seperate counter as N_BOOKS only indicates the maximum. Of course, the library file will be empty until you put something in it.

I expect you'll need a simple menu to allow the user to: add, search delete, show items.

If the user selects add, add one entry to the library. It seems you're trying to fill the library on add.
Topic archived. No new replies allowed.