#include <iostream>
usingnamespace std;
//the pin is 752634
//can withdraw or deposit only
int bankbalance(){
int pinNo;
cout << "Please key in your 6 digits PIN number.\n";
cin >> pinNo;
int Ttries = 3; //total number of tries
int Utries = 1; //counting with respect to the first user input
while(pinNo != 752634 && Ttries - Utries != 0){
cout << "You have enter an invalid PIN number.\n";
cout << "Please try again.\n";
Utries++;
cin >> pinNo;
}
if(pinNo != 752634 && Ttries - Utries == 0){
cout << "You have no more chances.\n";
cout << "Your accound has been locked.\n";
cout << "Please re-set your PIN before trying.\n";
}
return pinNo;
}
float amountleft(float moneytrans){ //moneytrans == money transaction
float initialamt;
initialamt = 90678; //amount of money one has in the account
while(initialamt - moneytrans <= 0){
cout << "ERROR\n";
cout << "You cannot withdraw that amount.\n";
cout << "\tThis is because your savings is less than amount to be withdrawn.\n";
cout << "Please key in a new amount you wish to withdraw.\n";
cin >> moneytrans;
if(initialamt - moneytrans >= 0){
cout << "Withdrawal successful." << endl;
cout << "You have " << initialamt - moneytrans << endl;
break;
}
float finalamt = initialamt - moneytrans;
return finalamt;
}
}
float amtincreased(float moneydep){
float initialamt;
initialamt = 90678;
float finalamt = initialamt + moneydep;
cout << "You have " << finalamt << " in your current account.\n";
return finalamt;
}
int main()
{
if(bankbalance() == 752634){
cout << "You have key in the correct PIN numbers.\n";
}else {
return 0;
}
cout << "Please proceed with your transactions.\n";
cout << "Please 1 of the following options:\n";
cout << "1. Withdrawal\n \b2. Deposit\n";
int Typeoftransaction;
cin >> Typeoftransaction;
if(Typeoftransaction == 1){
cout << "Please key in the amount you wish to withdraw.\n";
float amountwith;
cin >> amountwith;
amountleft(amountwith);
}elseif(Typeoftransaction == 2){
cout << "Please key in the amount you wish to deposit.\n";
float amountdep;
cin >> amountdep;
amtincreased(amountdep);
}
return 0;
}
As I am studying on my own, it is hard for me to determine which is the correct method. There are tons of advanced and complex answers online, which I cannot determine which is the right answer to my questions. I have entered them below for clarifications purposes.
I have the following questions:
1) The parentheses are meant for limits and boundary.
1 2
int bankbalance()
If I did not place any limits in it, it means that the code will just run on its own when called upon? When is it a good time to place limits and when is it not?
2) If I were to have a few functions, the name within each function can they be named the same? Why or why not?
3) How do choose between return functions and void functions? I know that return functions mean that you will get back a result, but you won't get one for void functions.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include <iostream>
usingnamespace std;
int question4(){
int qns4;
cout << "Please key in a number.\n";
cin >> qns4;
return qns4;
}
int main()
{
cout << question4();
return 0;
}
Is it possible to just get the value of qns4 instead of running the whole line of code? I am not so sure about the advanced codings just yet.
1) The parentheses after the function name are used to pass input to the function. If the function takes no input the parentheses will be empty.
2) Functions in the same scope can be named the same as long as the number of parameters or the type of the parameters are different. When you call one of these functions the compiler will check the type of the arguments to see which one that is a better match. This is called function overloading.
3) The function arguments are the inputs, and the return value is the output.
.----------.
aguments --> | Function | --> return value
`----------ยด
digging into best practices, it is really good to separate your user I/O and routines that do stuff. Talk to the user, get info, bring that info back, and then call the routines that process the info. If you did that, next week you can make a gui for your program and the processing routines stay the same, only the I/O needs to change.
initialamt is set every time the function is called. if you want it to keep its value between calls, you need to put the keyword static in front of it. but it would be better to pass this in; not everyone has the same initial amount, what if you were handling many accounts?
most of the time you use doubles, not floats, if you want a decimal value. and most of the time, you would use integers = pennies (in the USA, and maybe 1 = 100th of a penny in some cases) and avoid decimals entirely due to their inability to represent all values. Money / banking and similar code will usually use integers.
Nothing you have done is 'wrong'. These are some ideas on 'better' approaches generally.