withdraw problem

What am i doing wrong? I tried to withdraw 1.40 but it works well on 50c but not on the rest of it.. I really appreciate your help :D

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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
  #include <iostream>
#include <string>
#include <cstdlib> //system("cls")
using namespace std;

void mainmenu();
void endprogram();
void userinput();


int main()
{
	system("cls");
	mainmenu();
	//userinput();
	
	string input;
	double amount;
	double max = 2.55;
	
	cout <<"\n\n Press D or d if you want to deposit.";
	cout <<"\n Press W or w if you want to withdraw.";
	cout <<"\n ==> ";
	cin >> input;
	
	if ((input[0] == 'W')||(input[0] == 'w'))
	{
		cout <<"\n Please enter the amount (in RM) => ";
		cin >> amount;
		if(amount > max)
		{
			
			cout <<"\n Sorry, insufficient fund. Transaction cancelled.\n";
			cout << endl;
			mainmenu();
			
		}
		else
		{
			cout <<"\n Yes, Please collect your coins:\n ";
			int calc = amount;
			
			while (calc < max)
			{
				int fifty = calc / 0.50;
				calc = calc - fifty;
				cout <<"50c x "<< fifty << endl;
			
				int twenty = calc / 0.20;
				calc = calc - twenty;
				cout <<" 20c x "<< twenty << endl;
			
				int ten = calc / 0.10;
				calc = calc - ten;
				cout <<" 10c x "<< ten << endl;
				
				int five = calc / 0.05;
				calc = calc - five;
				cout <<" 5c x "<< five << endl;
			}
			

			
		
		}
	}
	else if ((input[0] == 'D')||(input[0] == 'd'))
	{
		cout <<"\n Please enter the amount (in RM) => ";
		cin >> amount;
		max == amount;
		cout <<"Max: RM "<< amount;
		
	}
	else
	{
		cout <<"\n Wrong input!\n";
		system("pause");
		system("cls");
		main();
	}
	
	endprogram();
	
	return 0;
}

void mainmenu()
{
	cout <<"      50c    20c    10c    5c  ";
	int cent50 = 0.50; 
	int cent20 = 0.20;
	int cent10 = 0.10;
	int cent5 = 0.05;
	
	int ini_cent50 = 4; 
	int ini_cent20 = 2; 
	int ini_cent10 = 1; 
	int ini_cent5 = 1;
	cout << endl;
	cout <<"      "<<ini_cent50<<"      "
			   <<ini_cent20<<"      "
			   <<ini_cent10<<"      "
			   <<ini_cent5;   
			   
	
	
	
}


void endprogram()
{
	string end;
	cout << endl;
	cout <<"\n Please press any key to continue,";
	cout <<"\n Press N or n to exit the program => ";
	cin >> end;
	
	if((end[0] == 'N')||(end[0] == 'n'))
	{
		cout<<"\n Thank you. Goodbye.";
	}
	else
	{
		main();
	}
	
}




Work with ints instead of doubles, the problem is that the numbers aren't being stored exactly. In addition, calc is decreasing and so will never be > than max. Check while > than 0 instead.

Same code but changed to int. Note that I only fixed the withdraw function as asked but the deposit function has issues as well.

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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include <iostream>
#include <string>
#include <cstdlib> //system("cls")
using namespace std;

void mainmenu();
void endprogram();
void userinput();


int main()
{
	system("cls");
	mainmenu();
	//userinput();

	string input;
	double amount;
	double max = 2.55;

	cout << "\n\n Press D or d if you want to deposit.";
	cout << "\n Press W or w if you want to withdraw.";
	cout << "\n ==> ";
	cin >> input;

	if ((input[0] == 'W') || (input[0] == 'w'))
	{
		cout << "\n Please enter the amount (in RM) => ";
		cin >> amount;
		if (amount > max)
		{

			cout << "\n Sorry, insufficient fund. Transaction cancelled.\n";
			cout << endl;
			mainmenu();

		}
		else
		{
			cout << "\n Yes, Please collect your coins:\n ";
			int calc = amount*100;

			while (calc > 0)
			{
				int fifty = calc / 50;
				calc = calc - fifty*50;
				cout << "50c x " << fifty << endl;

				int twenty = calc / 20;
				calc = calc - twenty*20;
				cout << " 20c x " << twenty << endl;

				int ten = calc / 10;
				calc = calc - ten*10;
				cout << " 10c x " << ten << endl;

				int five = calc / 5;
				calc = calc - five*5;
				cout << " 5c x " << five << endl;
			}




		}
	}
	else if ((input[0] == 'D') || (input[0] == 'd'))
	{
		cout << "\n Please enter the amount (in RM) => ";
		cin >> amount;
		max == amount;
		cout << "Max: RM " << amount;

	}
	else
	{
		cout << "\n Wrong input!\n";
		system("pause");
		system("cls");
		main();
	}

	endprogram();

	return 0;
}

void mainmenu()
{
	cout << "      50c    20c    10c    5c  ";
	int cent50 = 0.50;
	int cent20 = 0.20;
	int cent10 = 0.10;
	int cent5 = 0.05;

	int ini_cent50 = 4;
	int ini_cent20 = 2;
	int ini_cent10 = 1;
	int ini_cent5 = 1;
	cout << endl;
	cout << "      " << ini_cent50 << "      "
		<< ini_cent20 << "      "
		<< ini_cent10 << "      "
		<< ini_cent5;




}


void endprogram()
{
	string end;
	cout << endl;
	cout << "\n Please press any key to continue,";
	cout << "\n Press N or n to exit the program => ";
	cin >> end;

	if ((end[0] == 'N') || (end[0] == 'n'))
	{
		cout << "\n Thank you. Goodbye.";
	}
	else
	{
		main();
	}

}
Last edited on
Hi,

In addition, don't ever call main() , it is explicitly not allowed by the standard.
I tried to run it, but.. it doesn't stop running (infinity). Perhaps (calc>0) that cause the error.
I tried to run it, but.. it doesn't stop running (infinity). Perhaps (calc>0) that cause the error.


Calling main() will do that, running risk of stack overflow.

The code is full of mixing of different types, this demonstrates a lack of understanding between double and int:

int cent50 = 0.50;

If you want to convert a double to int, use static_cast<int>(/* the double value */)

This statement had no effect:

max == amount;

== is for comparison, = is for assignment.
So what shud i call instead of main if i want the program to redo 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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include <iostream>
#include <string>
#include <cstdlib> //system("cls")
using namespace std;

void mainmenu();
void endprogram();
void userinput();


int main()
{
	system("cls");
	
	cout << endl;
	mainmenu();
	
	
	string input;
	double amount;
	double max = 2.55;
	
	cout <<"\n\n Press D or d if you want to deposit.";
	cout <<"\n Press W or w if you want to withdraw.";
	cout <<"\n ==> ";
	cin >> input;
	
	
	
	
if ((input[0] == 'D')||(input[0] == 'd'))
	{
		int depo50, depo20, depo10, depo5;
		
		cout <<"\nHow many 50c coins? => ";
		cin >> depo50;
		//double amount2 = amount - depo50*0.50; // return balance of the amount input by user.
		
		cout <<"How many 20c coins? => ";
		cin >> depo20;
		//amount2 = amount - depo20*0.20; // return balance of the amount input by user.
		
		cout <<"How many 10c coins? => ";
		cin >> depo10;
		//amount2 = amount - depo10*0.10; // return balance of the amount input by user.
		
		cout <<"How many 5c coins? => ";
		cin >> depo5;
		//amount2 = amount - depo5*0.05; // return balance of the amount input by user.
		
		double total = depo10*0.10 + depo20*0.20 + depo5*0.05 + depo50*0.50; // return the total inputs of depo50, depo20, depo10, depo5.
		max == total;
		mainmenu();
		cout << endl;
		endprogram();
		
	}
	
else if ((input[0] == 'W')||(input[0] == 'w'))

	{
		cout <<"\n Please enter the amount (in RM) => ";
		cin >> amount;
		if(amount > max)
		{
			
			cout <<"\n Sorry, insufficient fund. Transaction cancelled.\n";
			cout << endl;
			mainmenu();
			
		}
		else
		{
			cout <<"\n Yes, Please collect your coins:\n ";
	
			if(amount >= 0.50)
				{
					int input_w = amount/0.50;
					amount = amount - (input_w*0.50);
					cout <<"50c x "<< input_w << endl;
				}
				if(amount >= 0.20)
				{
					int input_w = amount/0.20;
					amount = amount - (input_w*0.20);
					cout <<"20c x "<< input_w << endl;
				}
				if(amount >= 0.10)
				{
					int input_w = amount/0.10;
					amount = amount - (input_w*0.10);
					cout <<"10c x "<< input_w << endl;
				}
				if(amount >= 0.05) 
				{
					int input_w = amount/0.05;
					amount = amount - (input_w*0.05);
					cout <<"5c x "<< input_w << endl;
				}
				
				mainmenu();
				endprogram();
			} 
			
		}
	}
		/*if(total == amount)
		{
			endprogram();
		}	
		else if (total < amount) // Total inputs of all deposits are less than the amount.
		{
			cout<<"\nDeclined. The amount deposited was short.";
			endprogram();
		}
		else // Total inputs of all deposits are more than the amount.
		{
			cout<<"\nDeclined. The amount deposited was more than input.";
			endprogram();
		}*/
		
	
	else
	{
		cout <<"\n Wrong input!\n";
		endprogram();
	}
	
	
	
	return 0;
}

void mainmenu()
{
	cout <<"\n      50c    20c    10c    5c  ";
	double cent50 = 0.50; 
	double cent20 = 0.20;
	double cent10 = 0.10;
	double cent5 = 0.05;
	
	int ini_cent50 = 4; 
	int ini_cent20 = 2; 
	int ini_cent10 = 1; 
	int ini_cent5 = 1;
	
	cout <<"\n      "<<ini_cent50<<"      "
			   <<ini_cent20<<"      "
			   <<ini_cent10<<"      "
			   <<ini_cent5;   
			   
	
	
	
}


void endprogram()
{
	string end;
	cout << endl;
	cout <<"\n Please press any key to continue,";
	cout <<"\n Press N or n to exit the program => ";
	cin >> end;
	
	
	if((end[0] == 'N')||(end[0] == 'n'))
	{
		cout<<"\n Thank you. Goodbye.";
		cout<<"\n\n ********************************************";
	}
	else
	{
		main();
	}
	
}


1. I dunno what to replace that main function.
2. My withdraw is not doing its job.
Means, if i want to withdraw 1.40, it should give me 2 50cents and 2 20cents but instead it only give me 50cents.
3. How to update the mainmenu() function after i withdraw or deposit some money?

I really2 appreciate your help :D
Last edited on
Topic archived. No new replies allowed.