I have been working on a program that starts with the user entering an id (as in account id) and then a main menu pops up where they can choose between balance check, withdraw, deposit, and exit. The program is never supposed to end. It is supposed to keep looping back to the id screen every time a transaction is completed. What I am having problems with is when I enter the id, I don't know how to do any of the main menu options with only the account that I am using. Please Help. Here is my code.

#include <iostream>
#include "Account.h"
using namespace std;

//Show the main menu.
void showMenu()
{
	//Pull up the main menu.
	cout << "Main Menu" << endl;
	cout << "1: Check Balance" << endl;
	cout << "2: Withdraw" << endl;
	cout << "3: Deposit" << endl;
	cout << "4: Exit" << endl;
	cout << "Enter a Choice: ";
}
int main()
{
	//Create the ten accounts with Ids of 0 through 9 and initial balances of 100.
	//The Annual Interest Rate is not important in this exercise but to keep from having to modify the
	//functions that I have already made I will make the default annual interest rate .045.
	Account *pAccount0 = new Account(0,100,.045);
	Account *pAccount1 = new Account(1,100,.045);
	Account *pAccount2 = new Account(2,100,.045);
	Account *pAccount3 = new Account(3,100,.045);
	Account *pAccount4 = new Account(4,100,.045);
	Account *pAccount5 = new Account(5,100,.045);
	Account *pAccount6 = new Account(6,100,.045);
	Account *pAccount7 = new Account(7,100,.045);
	Account *pAccount8 = new Account(8,100,.045);
	Account *pAccount9 = new Account(9,100,.045);
	//Create an array for the account ID's to be stored in.
    int *accounts = new int[10];
	accounts[0] = (*pAccount0).getBalance();
	accounts[1] = (*pAccount1).getBalance();
	accounts[2] = (*pAccount2).getBalance();
	accounts[3] = (*pAccount3).getBalance();
	accounts[4] = (*pAccount4).getBalance();
	accounts[5] = (*pAccount5).getBalance();
	accounts[6] = (*pAccount6).getBalance();
	accounts[7] = (*pAccount7).getBalance();
	accounts[8] = (*pAccount8).getBalance();
	accounts[9] = (*pAccount9).getBalance();
	//Prompt the user to input the id.
	cout << "Enter an ID: ";
	int id;
	cin >> id;
	//If the id is greater than 9 it is not in the ATM.  Display an error message and prompt for ID again.
	while(id > 9)
	{
		cout << "Error:  That ID does not exist. Please try again." << endl;
		cout << "Enter an ID: ";
		cin >> id;
	}
	void showMenu();
	int choice;
	cin >> choice;
	//If choice is greater than four it is not one of the choices.  Prompt for another choice after displaying error.
	while (choice > 4)
	{
		cout << "Error:  That is not a valid choice. Please try again." << endl;
		cout << "Enter a Choice: ";
		cin >> choice;
	}
	double balance;
	int chosenId;
	double rate;
	//Set the account to the selected account values.
	switch (id)
	{
	case 0:
		balance = (*pAccount0).getBalance();
		chosenId =(*pAccount0).getId();
		rate = (*pAccount0).getAnnualInterestRate();
		break;
	case 1:
		balance = (*pAccount1).getBalance();
		chosenId = (*pAccount1).getId();
		rate = (*pAccount1).getAnnualInterestRate();
		break;
	case 2:
		balance =(*pAccount2).getBalance();
		chosenId =(*pAccount2).getId();
		rate =(*pAccount2).getAnnualInterestRate();
		break;
	case 3:
		balance =(*pAccount3).getBalance();
		chosenId =(*pAccount3).getId();
		rate =(*pAccount3).getAnnualInterestRate();
		break;
	case 4:
		balance =(*pAccount4).getBalance();
		chosenId =(*pAccount4).getId();
		rate =(*pAccount4).getAnnualInterestRate();
		break;
	case 5:
		balance =(*pAccount5).getBalance();
		chosenId =(*pAccount5).getId();
		rate =(*pAccount5).getAnnualInterestRate();
		break;
	case 6:
		balance =(*pAccount6).getBalance();
		chosenId =(*pAccount6).getId();
		rate =(*pAccount6).getAnnualInterestRate();
		break;
	case 7:
		balance =(*pAccount7).getBalance();
		chosenId =(*pAccount7).getId();
		rate =(*pAccount7).getAnnualInterestRate();
		break;
	case 8:
		balance =(*pAccount8).getBalance();
		chosenId =(*pAccount8).getId();
		rate =(*pAccount8).getAnnualInterestRate();
		break;
	case 9:
		balance =(*pAccount9).getBalance();
		chosenId = (*pAccount9).getId();
		rate =(*pAccount9).getAnnualInterestRate();
		break;
	}
//Main Menu Options.
	switch(choice)
	{
	case 1:
		cout << "The balance is: " << balance << endl;
		void showMenu();
		break;
	case 2:
		cout << "Enter an amount to withdraw: ";
		int withdraw;
		cin >> withdraw;
	}
}

Is it that you want the menu to be displayed according to the Account ID?
Can you please detail a little more about your problem.

Instead of doing a switch() on id, how about doing a switch on the menu choice? Then, it would be very simple to create a few functions that take the id as a parameter to do what you want.

e.g.,

//pseudo
switch(menu_choice)
{
case 1:
	CheckBalance(id);
	break;
case 2:
	Withdraw(id);
	break;
case 3:
	Deposit(id);
	break;
case 4:
	break;
}

This way, you're not "setting" anything (i.e., "//Set the account to the selected account values."). You can just pass an ID to your functions, which will then determine what action to take on which account. Unless I'm misunderstanding what you're trying to do?

>As another tip - try to keep your main() relatively small. I would suggest creating a couple of functions to InitializeAccounts() & ExecuteCommand(choice), or something similar. This makes accessing your data a little different, but it's worth the extra work.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.