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.
#include <iostream>
#include <string>
#include <cstdlib> //system("cls")
usingnamespace 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();
}
elseif ((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?