My main runs through the loop once and only goes to the if 1. It inputs, then pulls the main up and no matter what you choose, it goes back to the first option and then turns to an infinite loop. Why?
#include "book.cc"
#include "library.cc"
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <fstream>
usingnamespace std;
void menu();
int main(){
Library myLib;
Book myBooks;
string filename, temp;
int choice, answer=1;
ifstream ins;
ofstream outs;
while(answer != 0){
menu();
cin >> answer;
if (answer = 1){
cout<<"Enter the number 1 for data from a file, 2 for data from a keyboard: ";
cin >> choice;
cout << "\n";
if(choice == 1){
cout <<"Please enter the file name you'd like to use: ";
cin >> filename; // gets the filename
ins.open(filename.c_str());
if (ins.fail()){
cout << "\Error!";
ins.close();
}
myBooks.input(ins);
ins.close();
}
else{
myBooks.input(cin);
cout << "\n";
}
}
elseif(answer = 2){
myBooks.output(cout);
}
elseif(answer = 3){
myLib.OutAllPages();
}
elseif(answer = 4){
myLib.SortByTitle();
}
elseif(answer = 5){
myLib.SorthByDate();
}
elseif(answer = 6){
Book targ;
cout << "Enter the title correctly to search: ";
cin >> targ;
myLib.SearchByTitle(targ);
}
elseif(answer = 7){
myLib.OutAllBooks();
}
elseif(answer = 8){
myLib.RemoveBook(myBooks);
}
elseif(answer = 9){
myLib.SaveToFile();
}
}
}
void menu(){
cout << "1. Add a book into the collection: " << endl;
cout << "2. Output the total number of pages of all the books in the collection: " << endl;
cout << "3. Output a listing of all the books in the collection: " << endl;
cout << "4. Sort the collection by title: " << endl;
cout << "5. Sor the collection by copyright date: " << endl;
cout << "6. Search the collection for a particular title: " << endl;
cout << "7. Search the collection and output all the books by a particular author: " << endl;
cout << "8. Remove a book from the collection, based on a title the user offers: " << endl;
cout << "9. Back-up a record of the collection toa file: " << endl;
cout << "0. Quit: " << endl;
}
All your comparisons are using the assignment operator rather than the equality test operator.
One, '=', means you set it to the variable to that number, i.e. setting answer to 1.
Two, '==', means you compare the variable to that number, i.e. "is answer 1?"