What's wrong with my code ?

Hello every one

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

this is my code.cpp file

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

#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); }
else if (choice == 2 ) { deposit(a); user(a);   }
else if (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"


how can i solve this problem in my code ? :(

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


#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(); }
else if (choice == 2 ) { deposit(); user();   }
else if (choice == 3 ) { print(); user();  }
else {cout<< "See you later\n\n"; return 0; }
}


this my original code ( that's just accept one account )
Topic archived. No new replies allowed.