Banking Simulator Gone Wrong :(

EDIT: also, if you do pick 3, after you put how much you want to deposit, it completely skips the getline(cin, accountChoice)

If you run this program, and for the menuOption you type in a letter it starts freaking out! I know that is called something like an overflow or some type of error. The thing I don't know is how to fix this :( I have been beating my head against the wall for 2 hours! (too tired to think straight) I tried using conditional statements to check for it but they didn't work right :( I tried to set the variable to NULL if they put it wrong but that didn't work. I also tried resetting the variable to 0 and that didn't work. I can't think of anything else to do.

Help please, greatly appreciate it :)

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
/**************************
 * Banking Simulator v1.0 *
 * Author: G-wiz          *
 * Date: Nov. 4, 2011     *
 **************************/

#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int menuOption;
string accountChoice;

class bankAccount
{
public:
void mainMenu()
{
    cout << "Welcome to the Bank of Wiz" << endl << "To get started, please select an option from the menu:" << endl << endl;
    cout << "1. View Account Balance(s)" << endl << "2. Transfer Between Accounts" << endl << "3. Deposit" << endl;
    cout << "4. Withdrawal" << endl;
    cin >> menuOption;
    menu(menuOption);
}
void depositMethod()
{
        cout << "You have select: deposit." << endl << "Enter the amount you wish to deposit: ";
        cin >> deposit;
        cout << "Enter the account you would like to make the deposit to: " << endl;
        cout << "1. Checking" << endl << "2. Savings" << endl <<  "3. Credit" << endl;
        getline (cin, accountChoice);
}
void menu(int option)
{
    if(menuOption == 1 || menuOption == 2 || menuOption == 3 || menuOption == 4)
    {
        switch (menuOption)
        {
            case 1:
                break;
            case 2:
                break;
            case 3:
                depositMethod();
            case 4:
                break;
        }
    }
    else
    {
        cout << "Invalid choice. Please enter a choice 1, 2, 3, or 4." << endl;
        mainMenu();

    }
}
private:
    int checkingAccount;
    int savingsAccount;
    int creditAccount;
    int deposit;
}customer;

int main()
{
    customer.mainMenu();
}
Last edited on
From a newbie to a newbie, you don't seem to have any error handling built into this program to catch incorrect enteries from the user. You also don't use MAIN as the master rule here until a later call. Or is it that you are defining AND calling functions too soon? =+S
if you look at the bottom, in int main() I have an object customer using a function from the class bankAccount that starts the mainMenu(); of my program. All you see above is a class definition with functions, variables, and an object defined.
@gwiz

I changed your menuOption from an int to a char. By checking a char, it now does not starts freaking out!.
Hope this helps out..
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
/**************************
* Banking Simulator v1.0 *
* Author: G-wiz          *
* Date: Nov. 4, 2011     *
**************************/

#include <iostream>
#include <string>
#include <sstream>

using namespace std;
char menuOption = ' ';

string accountChoice;

class bankAccount
{
private:
	int checkingAccount;
	int savingsAccount;
	int creditAccount;
	int deposit;
public:
	void mainMenu()
	{
		while ( menuOption > 0 || menuOption < 5 )
		{
			cout << "Welcome to the Bank of Wiz" << endl << "To get started, please select an option from the menu:" << endl << endl;
			cout << "1. View Account Balance(s)" << endl << "2. Transfer Between Accounts" << endl << "3. Deposit" << endl;
			cout << "4. Withdrawal" << endl;
		
			menuOption = '0';
			cin >> menuOption;
			if (menuOption > '0' && menuOption < '5' )
			{
				menu(menuOption);
			}
			else
			{
				cout << endl << "That is not one of the options..." << endl << endl;
			}
		}
	}
	void depositMethod()
	{
		accountChoice = "0";
		cout << "You have select: deposit." << endl << "Enter the amount you wish to deposit: ";
		cin >> deposit;
		cout << "Enter the account you would like to make the deposit to: " << endl;
		cout << "1. Checking" << endl << "2. Savings" << endl <<  "3. Credit" << endl << endl;
		while ( accountChoice < "1" || accountChoice > "3")
		{
			getline(cin, accountChoice);
		}
	}

	void menu(int option)
	{
		switch (menuOption)
		{
		case '1':
			cout << "Option 1 requested."<< endl;
			mainMenu();
		case '2':
			cout << "Option 2 requested.."<< endl;
			mainMenu();
		case '3':
			depositMethod();
			mainMenu();
		case '4':
			cout << "Option 4 requested...."<< endl;
			mainMenu();
		default:
			cout << "Invalid choice. Please enter a choice 1, 2, 3, or 4." << endl << endl;
			mainMenu();
		}
	}
}customer;

int main()
{
	customer.mainMenu();
}
Last edited on
Topic archived. No new replies allowed.