Is it impossible to enter the private member variable directly as std::cin? Also, why do the following problems occur? I'm curious
Below are all the class header files.
class Bank
{
private:
int Account;
Char *Name; // will be received with dynamic allocation.
int Money;
public:
//Decide the first menu
voidShowMenu(void)const; // Let's show the menu.
void MadeAccount(); //1. Account opening
void InputAccount(); //2. Deposit
voidOutputAccount();//3. Withdrawal
void ShowAllAccount() const; // 4. Output all accounts
ball searchID (intid); // Process to check if the ID is correct
~Bank()
{
delete[]Name;
}
};
Below is a part of the class cpp file.
void Bank::MadeAccount()
{ cout <<"[Account opening]" <<endl;
cout << "Account ID: ";
sin >> Account; // point of issue1
char *name = 0 ;
cout << "Name:";
cin >> name; // point of issue2
Name = new char[strlen(name) + 1];
int money;
cout << "Income amount:";
Money = 0;
cin >> money;
Money += money;
}
There is a problem at the point where the problem occurred above.
Problem point 1 is
Problem point 2 is
The above problem is occurring... What should I do?
c++
We didn't allocate memory to the name, so you can allocate it in advance as below.
char *name = 0 ;
Please change it as below.
char *name = new char[20];
But since we don't know how long we're going to get 20, I think using std:string to manage the strings is one way.
private:
//char *Name;
string Name;
...
void Bank::MadeAccount() {
...
// // char *name = 0 ;
// cout << "Name:";
// cin >> name; // point of issue2
// // Name = new char[strlen(name) + 1];
string name;
cout << "Name: ";
cin >> name;
Name = name;
...
}
If you use string, you can delete the name from the destructor.
~Bank()
{
// // delete[]Name;
}
© 2024 OneMinuteCode. All rights reserved.