1 Replies - 1213 Views - Last Post: 25 September 2011 - 09:21 AM Rate Topic: -----

#1 jlharri9  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 25-September 11

Using switch menus with arrays

Posted 25 September 2011 - 08:05 AM

Hi, I am a newbie to C++ and this is my introduction class. I have been given this prompt:

In this project we will use arrays to add a summary report to the ATM program. Using Project 3 as a base, add functionality that allows you to complete a report at the end of the session. The report should include output based on the following:

Transaction Number Transaction Type Balance Amount New Balance

1 Deposit $500.00 $100.00 $600.00

2 Withdrawal $600.00 $200.00 $400.00



Final Balance: $400.00



There is a limit of 10 transactions to a statement. Note that calls to the check balance and the exit call do not count as transactions in the overall transaction count displayed at the end of the program.

Assume the same Initial balance of $ 500.00. All numbers displayed in output should be in dollars and cents format ($XX.YY).
Below is my code.

#include <iostream>
#include <iomanip>
using namespace std;

void displayMenu ( );
void getTransaction(int [], int);
double getBalance(double [], int);
const int SIZE = 10;
double newBalance( double [], int );
int main ()

{  
	
	
	
	double amount[SIZE];
	int selection[SIZE];
	double balance[SIZE] = {500.00};
	cout << setprecision(2) << fixed << endl;

	//Do while statement for menu selection
	do
	{
	displayMenu ();
	cout << "Which option would you like to select? " <<endl;
	cin >> selection[SIZE];
	

	
	switch (selection[SIZE])
	{
	case 1:
		cout << "How much would you like to deposit or withdraw? $";
		for ( int count = 0; count < SIZE; count++)
		{
			cin >> amount[count];
			balance[count] = balance[count]+ amount[count];
		}

	case 2:
		cout << "How much would you like to deposit or withdraw? $";
		for ( int count = 0; count < SIZE; count++)
		{
			cin >> amount[count];
			balance[count] = balance[count]- amount[count];
		}
	case 3:
		cout << "Your balance is $" << balance[SIZE] <<endl;
		continue;

	case 4:	
		cout << "Good-bye!" << endl;
		cout << endl;
		cout << "Transaction # " << setw(15) << "Transaction Type " << setw(13) << "Balance" << setw(13) <<
		"Amount" << setw(15) << "New Balance" << endl;
		cout << "----------------------------------------------------------------------------"<< endl;

	for (int count = 0; count < SIZE; count++)
	{ 
		cout << count + 1 << setw(25);
		getTransaction(selection, count);
		cout << setw(15)<< balance[count] << setw(15) << amount[count] << setw(13) << newBalance(balance,SIZE) << endl;
		
	}
	break;
	} 
	}while (selection[SIZE] !=4);
	system ("pause");
	
	}
	

void displayMenu ()
	{
	cout << "Account Menu" << endl; 
	cout << "-------------------" << endl;
	cout << "1. Deposit" << endl;
	cout << "2. Withdrawal" << endl;
	cout << "3. Check Balance"<< endl;
	cout << "4. Exit" << endl;

	}
	
void getTransaction(int selection[], int SIZE)
{
  
	if (selection[SIZE] == 1)
	   cout << "Deposit";
   else (selection[SIZE] ==2);
	   cout << "Withdrawal";
	
}


double newBalance( double newBalance[], int SIZE)
{
	double balance[10] = {500.00};
	for (int count = 0; count < SIZE; count++)
		return newBalance[count] = balance[count];
}




When I compile I get no error messages. The trouble I'm having is how to store the selection values in an array without a for loop that which requires me to enter all the selections at that moment. I need to be able to select a value, store it in the array, process information from that selection and then repeat the action.

And just like the selection array, for the amount of deposit and withdrawal, I just want to store the value in the array, process that amount, and then repeat but I cannot figure out how to store the values in the array without the loop.

Also I have looked up ways to solve the problem but they all suggest to use pointers which we have not covered in class and are not supposed to use to solve the program.

Thanks for your help in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: Using switch menus with arrays

#2 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1274
  • View blog
  • Posts: 4,393
  • Joined: 19-February 09

Re: Using switch menus with arrays

Posted 25 September 2011 - 09:21 AM

Hi. Here you are confusing the menu selection with the transaction number, they are not quite compatible.

  cout << "Which option would you like to select? " <<endl;
  cin >> selection[SIZE];



So a simple variable to hold menu selection would be easier.

What data do you need to hold?
Transaction Number - this could be calculated from the index to the arrays.

Transaction Type - you are using 1 and 2 at the moment.
Transaction Balance - before transaction
Transaction Amount - says it all
Transaction New Balance - after transaction

So to hold each transaction record you will need arrays for the four/five items.

This post has been edited by #define: 25 September 2011 - 09:23 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1