ATM (beginner) debit program

So I have one main concern, whenever I run the program and choose a selection, it will not allow me to input any information. It simply loops back to the selection menu. I had this problem when creating the code, but had to complete the assignment. I am now trying to fix it as we are in our testing phase. I have seen several programs similar but I am not sure what is wrong with mine. Any help would be greatly appreciated!

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
/************************ 
AccountTestMain.cpp
Ashley Godfrey
9/21/2014
Assignment3

DESCRIPTION
***********************/

#include <iostream>
#include <iomanip> //for 'fixed' and 'setprecision'

//funciton main begins program execution

int main ()
{
double account1 = 100;
double account2 = 50;

double withdrawal;
double withdraw;
double transferOne;
double transferTwo;

//display initial balance of each account

std::cout << "Account 1 balance: $" << std::setprecision(2) << std::fixed << account1 << std::endl;
std::cout << "Account 2 balance: $" << std::setprecision(2) << std::fixed << account2 << std::endl;


int selection = 0; //variable to hold users choice
int transaction = 0; //variable to track the number of transactions
while (selection != 5)
{
 //Display a menu to the user

std::cout << "(1) to debit account 1" << std::endl;
std::cout << "(2) to debit account 2" << std::endl;
std::cout << "(3) to transfer from account 1 to account 2" << std::endl;
std::cout << "(4) to transfer from account 2 to account 1" << std::endl;
std::cout << "(5) to exit" << std::endl; //sentinel value

//ask user for their choice


	break;
case 5:
		//do nothing, the while loop exits when it evaluates the expression
	break;

}

transaction += 1;

//display balance after each transaction
//do not change these two statements
////////////////////////////
////////////////////////////
std::cout << "[" << transaction << "] Account 1 balance: $" << std::setprecision(2) << std::fixed << account1 << std::endl;
std::cout << "[" << transaction << "] Account 2 balance: $" << std::setprecision(2) << std::fixed << account2 << std::endl;
std::cout << std::endl;

	} 
} //end main

Last edited on
If I choose the first menu, is this program print "Enter debit amount for account 1: " ?
I'm online from my phone so I can't test it myself
yes, it is supposed to allow the user to input the amount they want to withdraw from the account but it keeps running right through back to the main menu.
Not sure that this will fix it, but why do you have those while statements in your switch code? By going to case 1:, for example, selection == 1 is already guaranteed.
honestly I didn't start it like that, I was trying to "experiment" with a few different things to see why it isn't working correctly.
I think while statement on the line 53 and 67 is uneeded
And on the line 57 you're using relational operator
But still i don't understand why the cin part is skipped ._. Maybe someone else here understand why, sorry

Try to use
1
2
using std::cin
using std::cout
instead of writng std:: each time you wrte cin or cout
i fixed the operators, that definitely took out quite a bit of errors I had. My professor has her code with std::cout and std::cin in all of her examples and guidelines so that's the only reason I had mine like that.

still no pause for user input though...
Does the cout << in the switch work?
hyperfine- only if i enter 1, for any other number (other than 5) the output balance for account1 & account2 show and it repeats the menu
okay I believe I may have figured out the kinks! thanks for the help.. unless someone has any advice on it still debiting when there is not enough money....
but here it is updated...
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/************************ 
AccountTestMain.cpp

Assignment3

DESCRIPTION
***********************/

#include <iostream>
#include <iomanip> //for 'fixed' and 'setprecision'

//function main begins program execution

int main ()
{
double account1 = 100;
double account2 = 50;

double withdrawal;
double withdraw;
double transferOne;
double transferTwo;

//display initial balance of each account

std::cout << "Account 1 balance: $" << std::setprecision(2) << std::fixed << account1 << std::endl;
std::cout << "Account 2 balance: $" << std::setprecision(2) << std::fixed << account2 << std::endl;


int selection = 0; //variable to hold users choice
int transaction = 0; //variable to track the number of transactions
while (selection != 5)
{
 //Display a menu to the user

std::cout << "(1) to debit account 1" << std::endl;
std::cout << "(2) to debit account 2" << std::endl;
std::cout << "(3) to transfer from account 1 to account 2" << std::endl;
std::cout << "(4) to transfer from account 2 to account 1" << std::endl;
std::cout << "(5) to exit" << std::endl; //sentinel value

//ask user for their choice

std::cout << "What would you like to do? ";
std::cin >> selection;

//use a switch statement to detect their option

switch (selection)
{
case 1:
	std::cout << "Enter debit amount for account 1: ";
	std::cin >> withdrawal;

	account1 -= withdrawal;

	if (account1 >= withdrawal)
			std::cout << "Subtracting " << std::setprecision(0) << std::fixed << withdrawal << " from account 1";
	else
		std::cout << "Error: Debit amount exceeds balance.";
	break;
case 2:
	{
std::cout << "Enter withdrawal amount for account 2: ";
	std::cin >> withdraw;

	if (account2 >= withdraw)
	{
		std::cout << "Subtracting " << std::setprecision(0) << std::fixed << withdrawal << " from account 2";
	}
	else
	{
	std::cout << "Error: debit amount exceeds balance.";
	}
}
	break;
case 3:
	std::cout << "Enter the amount you would like to transfer from account 1 to 2: ";
	std::cin >> transferOne;
	account1 -= transferOne;
	account2 += transferOne;

	break;
case 4:
		std::cout << "Enter the amount you would like to transfer from account 2 to 1: ";
		std::cin >> transferTwo;
		std::cout << std::endl;
		account2 -= transferTwo;
		account1 += transferTwo;

	break;
case 5:
		//do nothing, the while loop exits when it evaluates the expression
	break;

}

transaction += 1;


//display balance after each transaction
//do not change these two statements
////////////////////////////
////////////////////////////

std::cout << std::endl << std::endl;
std::cout << "[" << transaction << "] Account 1 balance: $" << std::setprecision(2) << 
std::fixed << account1 << std::endl;
std::cout << "[" << transaction << "] Account 2 balance: $" << std::setprecision(2) << 
std::fixed << account2 << std::endl;
std::cout << std::endl << std::endl;
//////////////////////////////////////
	} 
} //end main 

It's because you already remove the money before checking to see if there is enough money in the account, I think. Put the account1 -= withdrawal; line in the if statement. You'll have to introduce curly brackets, of course.
debiting from account 1 -

line 55 - you're subtracting the withdrawal amount before you check to see if there's enough money at line 57

debiting from account 2 -

line 69 - you're printing out the value of withdrawal instead of the withdraw that the person input on line 65. And you're not actually debiting account2.


transferring between accounts - should you be checking that the account has a sufficient balance to be able to transfer the amount requested?
thank you:)
Topic archived. No new replies allowed.