I've written out the code for an assignment for school, but it isn't working as it should. Everything compiles and runs, but I run into a couple of issues. Neither of the first couple of menu options return anything on screen. It almost makes me thing I've done something wrong when loading my object array from the file.
I've looked this over for the last couple of hours, and can't seem to find out what I'm doing wrong, so I'm turning to people with far more experience that I have.
*Edited to cut out some of the code that may be unnecessary for you to help with.
Here are the functions that seem to be giving me problems. They are called in a switch in the main program.
Here is main()
int main()
{
//Arrays
BookClass books[MAX_BOOKS];
//Variables
int choice; //For menu selection from getMenuChoice.
int bookNum; //For holding array location of book for the findBook function.
int numBooks; //Holds the number of books in the array
string searchTitle; //Hold value of user's search for findBook function.
BookClass newBook;
fstream file; //For input of file to load array
getBooks(books, numBooks);
do
{
choice = getMenuChoice();
if (choice != 6)
{
switch (choice)
{
case 1:
{
showBooks(books, numBooks);
break;
}
case 2:
{
showTitles(books, numBooks);
break;
}
case 3:
{
cout << "\nEnter the title you wish to find: ";
getline(cin, searchTitle);
cin.ignore();
findBook(searchTitle, books, numBooks);
bookNum = findBook(searchTitle, books, numBooks);
break;
}
case 4:
{
cout << "\nEnter the title you wish to check out: ";
getline(cin, searchTitle);
cin.ignore();
findBook(searchTitle, books, numBooks);
bookNum = findBook(searchTitle, books, numBooks);
books[bookNum].returnBook();
break;
}
case 5:
{
cout << "\nEnter the title you wish to return: ";
getline(cin, searchTitle);
cin.ignore();
findBook(searchTitle, books, numBooks);
bookNum = findBook(searchTitle, books, numBooks);
books[bookNum].checkOutBook();
break;
}
}
}
if (choice == 6)
{
cout << "\nThank you for using the library program!! This program will now end!" << endl;
}
} while (choice != 6);
system("pause");
return(0);
}
And here are the functions that seem to be giving me issues...
//Function definition for getBooks. Loads the 'books' array using data from input file 'books.txt'
void getBooks(BookClass books[], int &numBooks)
{
string bookTitle, authorName, bookPublisher, bookISBN;
double bookPrice;
int bookYear, bookInStock;
int total = 0;
ifstream file;
file.open("books.txt", ios::in);
if (file.fail())
{
cout << "\nThe file failed to open. Please restart the program and try again!" << endl;
system("pause");
return;
}
for (int i = 0; i < numBooks; i++)
{
getline(file, bookTitle);
getline(file, authorName);
getline(file, bookPublisher);
getline(file, bookISBN);
file >> bookPrice;
file >> bookYear;
file >> bookInStock;
books[i].storeBook(bookTitle, authorName, bookPublisher, bookISBN, bookPrice, bookYear, bookInStock);
books[i].displayBookInfo();
numBooks++;
}
file.close();
}
//Function definition for showBooks. Loops through the 'books' array and displays info for all books to user.
void showBooks(BookClass books[], int numBooks)
{
for (int i = 0; i < numBooks; i++)
{
books[i].displayBookInfo();
cout << endl;
}
}
//Function definition for showTitles. Similar to showBooks. Only displays the titles of the books to the user.
void showTitles(BookClass books[], int numBooks)
{
for (int i = 0; i < numBooks; i++)
{
books[i].getTitle();
cout << endl;
}
}
Any help is appreciated. Sorry if any formatting or anything of that nature isn't done quite correctly. I'm very new around here. So any critique for the future is also welcome and appreciated!