While loop infinite after second loop

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?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include "book.cc"
#include "library.cc"

#include <iostream> 
#include <cstdlib>
#include <iomanip>
#include <fstream>

using namespace 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";
				}
		}	
		else if(answer = 2){
				myBooks.output(cout);
			}	
		else if(answer = 3){
				myLib.OutAllPages();
			}
			
		else if(answer = 4){
				myLib.SortByTitle();
			}
			
		else if(answer = 5){
				myLib.SorthByDate();
			}
			
		else if(answer = 6){
				Book targ;
				cout << "Enter the title correctly to search: ";
				cin >> targ;
				myLib.SearchByTitle(targ);
			}
			
		else if(answer = 7){
				myLib.OutAllBooks();
				
				
			}
			
		else if(answer = 8){
				myLib.RemoveBook(myBooks);
			}
			
		else if(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?"
Last edited on
Topic archived. No new replies allowed.