There was a "project" at my uni where i had to make a program that uses two constructors, a default one that sets some default values to the general parameters and another one where the user has the chance to put some values himself.
Now my problem is (note that i am new to c++ and I need some guidance) that i do not know how to validate these parameters, for example the balance must be > 0 and i do not know how to validate it. Also there's an update() function that at the end of each year, a certain amount of money is added to the account depending on the rate of the account.
#include <iostream>
usingnamespace std;
class BankAccount{
public:
BankAccount(){ //Default constructor
Balance = 0;
Rate = 0;
Account_id = 111111111111;
}
BankAccount(double balance, double rate, unsignedlonglongint account_id){ //Retrieve parameters.
Balance = balance;
Rate = rate;
Account_id = account_id;
do{ //Validate the Account ID the user enters
cout << "Type a twelve-digit Account ID. \n";
cin >> account_id;
}while(account_id <= 111111111111 && account_id >= 99999999999);
do{ //Validate the Balance value the user enters
cout << "Type a valid Balance number. *Balance > 0 \n";
cin >> balance;
}while(balance < 0);
do{ //Validate the Rate value the user enters
cout << "Type a valid rate number. ** 0 < rate < 1 ** \n";
cin >> rate;
cout <<endl;
}while(0 < rate < 1);
cout << "Bank Account ID: "<< account_id << "." << endl;
cout << "Current Account Balance: " << balance << " euro(s)." << endl;
cout << "Account Rate: " << rate << " ." << "\n"<< endl;
}
void setBalance(longdouble balance){ Balance = balance;} //Function that sets th balance value
double getBalance(){return Balance;} //Function that gets the balance value
void setRate(longdouble rate){Rate = rate;} //Function that sets the rate value
double getRate(){return Rate;} //Function that gets the rate value
void setAccount_id(unsignedlonglongint account_id){Account_id = account_id;} //Function that sets the Account ID
unsignedlonglongint getAccount_id(){return Account_id;} //Function that gets the Account ID
void update(){
int apantisi;
double rate, balace;
cout << "Did a year pass? If yes type 1, else 0. \n";
cin >> apantisi;
cout << "\n";
if(apantisi == 1){
double afterbalance, balance;
afterbalance = 0;
afterbalance = afterbalance + (balance*rate) /100;
balance = balance + afterbalance;
cout << "Balance has been increased to " << balance << " euro(s)." << "\n" << endl;
}
if(apantisi == 0){
cout << "Balance has not been increased" << endl;
}
};
private:
double Balance, Rate;
unsignedlonglongint Account_id;
};
int main()
{
BankAccount object();
object.update();
return 0;
}
So when a user puts some "invalid" values in the object, I do not know how to actually prompt him to re-put some values, the do-while way works but only if i use only 1 constructor.
The deadline is over and the code i sent was wrong, hopefully i will get some points for the effort, but i still want to learn how to do it.
Thank you for the effort in reading this and sorry if I gave you a headache with this code.
First why are you trying to get all the data inside the constructor? That constructor has parameters that you should be using to pass the required values into the constructor.
Next you called the constructor that didn't have arguments so your variables will all have the default values.
The first constructor will put the default values in that data, what do you mean by "all data".
Second, if i remove the do-while on the other constructor, i dont know how to validate the data a user will put, for example if a user gives negative balance..
Second, if i remove the do-while on the other constructor, i dont know how to validate the data a user will put, for example if a user gives negative balance..
That's why I recommend not getting the user input in that constructor.
The first constructor will put the default values in that data, what do you mean by "all data".
You do realize that only one of your constructors will ever be called, right? In the code you provided the only constructor that is being called is the constructor with no arguments. The constructor with multiple arguments is never being called.
Actually I disagree about calling the multiple argument constructor from the no-argument constructor. The no argument constructor should not need to check the values because the programmer insured that the default arguments were valid when he wrote the program. You should only need to validate the values when the user is providing them.