Infinite Loop, Print Menu repeating

My code keeps repeating a loop of the menu:
Someone know how to solve it?
This is one .cpp there is another and the header

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
#include <iostream>
#include "bankingSystem.h"

//Print a menu of options to the user.

void PrintMenu()
{

    std::cout << "\n(1) Add Account\n";
    std::cout << "(2) Delete Account\n";
    std::cout << "(3) Print Accounts\n";
    std::cout << "(4) Deposit\n";
    std::cout << "(5) Withdrawal\n";
    std::cout << "(6) Exit\n";
    std::cout << "\nEnter selection:  ";

}

//Start your main program loop here.


int main()
{

    //All class functions are called through a BankingSystem object.

    
    BankingSystem b;
    std::cout << "\n*** Welcome to the Banking System ***\n";

	PrintMenu();

	int selection;



	std::cin >> selection;

	while ( selection !=6 )
	{
		switch ( selection )
		{
		case 1:
			&BankingSystem::CreateAccount;
			break;
		case 2:
			&BankingSystem::DeleteAccount;
			break;
		case 3:
			&BankingSystem::PrintAccounts;
			break;
		case 4:
			&BankingSystem::Deposit;
			break;
		case 5:
			&BankingSystem::Withdraw;
			break;
		default:;
			std::cout<< "Incorrect selection.\nPlease enter a number between 1 and 5 or 6 to exit\n";
			break;
		}

		PrintMenu();
	}
	

    return 0;

}
Variable selection is not changed inside the while loop. As the result you have the infinite loop.
Last edited on
Changed the
&BankingSystem::CreateAccount;
to
b.CreateAccount();

now the problem is that when it starts, the first time ok but from the second will always choose the first choice.
Like i open and pressed 1
from now on always will choose 1 when back to menu


And if i put anything beside 1 to 6, it creates another loop
Last edited on
as vlad alluded to, put the input (line 37) just inside the while loop. Also, you probably want to put the PrintMenu function call at the start of the while loop as well, or else it's going to display after you select 6.
Topic archived. No new replies allowed.