If Else problem

When i pass choice "2" why my program proceed to last else?It should have to stop.
#MindStuck
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
void Booklist()
{
	int choice;
	ifstream books;
	books.open("Book list.txt");
	string temp;
	cout << "\n\n\t\tAvalible Books Are";
	cout << "________________________________________________________________________________\n";
	while(!books.eof() )
	{

		getline(books,temp);
		cout<<"\n"<<temp;
		getline(books,temp);
		cout<<temp;
		cout<<endl;
	}
	cout << "________________________________________________________________________________\n";
	cout<<"\n\n1-Main Menu  2-Exit\n";
	cin.ignore();
	cin >> choice;
	if(choice==1){main();}else{
		if(choice==2){cout << "\tThank you for using this Program :) ";}
		else{ cout << "Invalid Choice :( ";cout<<"\n\nYou are terminated to Library Main Menu \n\n";Library();}
	}
}
closed account (18hRX9L8)
You should clean up your test statements... This is my preferred style of writing conditionals:

1
2
3
4
5
6
7
8
9
10
11
	//.........

	if (choice == 1) {
		main();
	} else if (choice == 2) {
		cout << "\tThank you for using this Program :) ";
	} else {
		cout << "Invalid Choice :( \n\nYou are terminated to Library Main Menu \n\n";
		Library();
	}
}


Also, you should never call main! I suggest taking the code from main and putting it in a function, and then calling that function from main and replacing any calls to main with calls to the new function.
Last edited on
I don't know where you are learning C++ from, but it's terrible. Get a goob book.
Topic archived. No new replies allowed.