I'm a beginner in c++ , and i'm trying to create my little ATM program
I started with simple program that's accept only one account for one person
then i try now to expand my code to receive more than one account
#include "bank.h"
Account::Account(){
int i=0;
cout << "To create account enter 1\nTo exit enter any button else : ";
cin >> i;
if (i==1)
create();
}
void Account::set(string n[],int x[],int y[]){
int i =0;
while (i < numberOfAccount){
name[i] = n[i];
balance[i]=x[i];
password[i] =y[i];
i++; }
}
string Account::getName(int i){
return name[i];
}
int Account::getBalance(int i){
return balance[i];
}
int Account::getPassword(int i){
return password[i];
}
void Account::print(int a){
cout<<"your balance is "<<getBalance(a)<<endl;
}
void Account::withdraw(int i){
int Getfrom;
cout << "Enter how much you need : \n";
cin >> Getfrom;
if (balance[i] >= Getfrom) { balance[i] = balance[i] - Getfrom ; }
else cout << "You don't have enough balance\n";
print(i);
}
void Account::deposit(int i){
int addto;
cout << "Enter how much you want to add : \n";
cin >> addto;
balance[i] +=addto;
print(i);
}
int Account::userStartup(){
string n[1];
int y[1] ;
cout<<"Enter your name "<<endl;
cin >>n[0];
cout<<"enter your password"<<endl;
cin>>y[0];
for (int i = 0 ; i < 10 ; i++){
if( n[i] == getName(i) and y[i] == getPassword(i) ) {user(i);}
else { cout<<"one of the name or the password is wrong"<<endl;
userStartup(); } }
return 0;
}
void Account::create(){
string n[50]={0};
int x[50]={0};
int y[50]={0};
int i=0;
int r=0;
while (r==0) {
cout << "Enter your name : "<<endl ;
cin >>n[i];
cout << "Enter your balance : "<<endl;
cin >> x[i];
cout << "Enter your Password : "<<endl;
cin >>y[i];
set(n,x,y);
cout <<"Congratulations "<<endl;//<<password;
cout << "Enter 0 to create another account\nenter any button else to get or add : ";
cin >> r;
i++;
}
startup();
}
void Account::startup(){
int x=0;
cout <<"enter 1 to add or deposit \nenter any other button to exit : "<<endl ;
cin >> x;
if (x==1) userStartup();
}
int Account::user(int a){
int choice;
cout << "\nTo get money enter 1";
cout << "\nTo add money enter 2 \nTo Show your balance enter 3\n enter any button else to exit "<<endl;
cin >> choice;
if (choice == 1) { withdraw(a); user(a); }
elseif (choice == 2 ) { deposit(a); user(a); }
elseif (choice == 3 ) { print(a); user(a); }
else {cout<< "See you later\n\n"; return 0; }
return 0;
}
but when I press one to create an acoount the compiler give me an error(logix one)!
this is the error
"terminate called after throwing an instance of 'std::logic_error' what(): basic_string::S_construct null not valid"
#include "bank.h"
Account::Account(){
int i=0;
cout << "To create account enter 1\nTo exit enter any button else : ";
cin >> i;
if (i==1)
create();
}
void Account::set(string n,int x,int y=0){
name =n;
balance=x;
password =y;
}
string Account::getName(){
return name;
}
int Account::getBalance(){
return balance;
}
int Account::getPassword(){
return password;
}
void Account::print(){
cout<<"your balance is "<<getBalance()<<endl;
}
void Account::withdraw(){
int Getfrom;
cout << "Enter how much you need : \n";
cin >> Getfrom;
if (balance >= Getfrom) { balance = balance - Getfrom ; }
else cout << "You don't have enough balance\n";
print();
}
void Account::deposit(){
int addto;
cout << "Enter how much you want to add : \n";
cin >> addto;
balance +=addto;
print();
}
int Account::userStartup(){
string n;
int y ;
int compar;
cout<<"Enter your name "<<endl;
cin >>n;
if (n=="-1"){return 0;}
cout<<"enter your password"<<endl;
cin>>y;
if( n == getName() and y == getPassword() ) {user();}
else { cout<<"one of the name or the password is wrong"<<endl;
userStartup(); }
}
void Account::create(){
string n;
int x=0;
int y=0;
cout << "Enter your name : "<<endl ;
cin >>n;
cout << "Enter your balance : "<<endl;
cin >> x;
cout << "Enter your Password : "<<endl;
cin >>y;
set(n,x,y);
cout <<"Congratulations "<<endl;//<<password;
startup();
}
void Account::startup(){
int x=0;
cout <<"enter 1 to add or deposit \nenter any other button to exit : "<<endl ;
cin >> x;
if (x==1) userStartup();
}
int Account::user(){
int choice;
cout << "\nTo get money enter 1";
cout << "\nTo add money enter 2 \nTo Show your balance enter 3\n enter any button else to exit "<<endl;
cin >> choice;
if (choice == 1) { withdraw(); user(); }
elseif (choice == 2 ) { deposit(); user(); }
elseif (choice == 3 ) { print(); user(); }
else {cout<< "See you later\n\n"; return 0; }
}
this my original code ( that's just accept one account )