I has problems for calling function CreateAccount() in this case
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
int toppingmoney_, withdraw_;
int account_number;
// Tao moi tai khoan:
// Nhap so tai khoan, ten chu tai khoan, loai tai khoan
// va so du ban dau tu ban phim
void Account::CreateAccount()
{
cout << "Nhap so tai khoan: ";//accountID
cin >> account_number_;
cout << "Nhap ten chu tai khoan: ";//userID
cin.getline(name_, 100);
cout << "Nhap loai tai khoan: ";//type of account
cin >> type_;
cout << "So du ban dau: ";//balance
cin >> balance_;
}
When I press 1 it just return to menu. Moreover, if I try to press other number like 2, 3 ,4,5,6,7,8 (number of other menu_option) it will right away go to that choice without any input to CreateAccount().
Can anyone explain to me what wrong in my logic here? Thanks a lot!
-PoorBoiz
Which CreateAccount are you talking about (or wanting to call)?
The class member function CreateAccount() to read user input and update class member variables?
The global function CreateAccount() to save something to account.dat ?
Note that the account you save is the default initialised local variable 'account' on line 3, and NOT some other variable called 'account' which you might have set up elsewhere.
I think that the CreateAccount() is also save it to the account.data file and I think that Create Account could let user input the information for their account instead of read from them? Or do I misunderstand something here?
Why do we need SavetoFile here? It's quite new to me, could you please explain more about that?
I think that the CreateAccount() is also save it to the account.data file and I think that Create Account could let user input the information for their account instead of read from them? Or do I misunderstand something here?
You can do all those things, if you write the code to make it do those things.
But magic doesn't happen just because you make the names of all the functions involved the same name.
I apologize I made a huge mistake their. The original source code has int account_number; for the instance of those case in switch. Here I show you parts of those cases.
void SearchAndPrintAccount(int account_number)
{
Account account;
bool found = false;
ifstream in_file;
in_file.open("account.dat", ios::binary);
if (!in_file)
{
cout << "Khong mo duoc file, hay an phim enter de quay lai menu" << endl;
system("pause");
Menu();
return;
}
cout << "\nKet qua tim kiem:\n";
while (in_file.read(reinterpret_cast<char *> (&account), sizeof(Account)))
{
SearchAndPrintAccount(account_number);
}
in_file.close();
if (found == false)
cout << "\n\nKhong tim thay tai khoan";
}
And it will work for
1 2 3 4
case'4':
cout << "\n\n\tNhap so tai khoan: ";//input account number
SearchAndPrintAccount(myAccount.GetAccountNumber());
break;
void SearchAndPrintAccount(int account_number)
{
Account account;
bool found = false;
ifstream in_file;
in_file.open("account.dat", ios::binary);
if (!in_file)
{
cout << "Khong mo duoc file, hay an phim enter de quay lai menu" << endl;
system("pause");
// Menu(); you're returning to Menu anyway, no need for recursion
return found;
}
cout << "\nKet qua tim kiem:\n";
while (in_file.read(reinterpret_cast<char *> (&account), sizeof(Account)))
{
// compare account_number with that read from the file
if ( account.GetAccountNumber() == account_number) {
cout << "Found it" << endl;
found = true;
}
}
in_file.close();
if (found == false)
cout << "\n\nKhong tim thay tai khoan";
}
After testing, I realized that the variable Account account in SearchAndPrintAccount(int account_number) does not have any value in it. How could I relate it to the one that I just create by using function CreateAccount()?
Did you end up with a file called account.dat when you did this?
1 2
myAccount.CreateAccount(); // reads from user
SaveToFile(myAccount); // saves to file
Your SaveToFile contains no error message if the file can't be opened.
You could put some extra output in SearchAndPrintAccount just to see if it's working as you intend.
1 2 3 4 5 6 7 8 9
while (in_file.read(reinterpret_cast<char *> (&account), sizeof(Account)))
{
cout << "Processing account number = " << account.GetAccountNumber() << endl;
// compare account_number with that read from the file
if ( account.GetAccountNumber() == account_number) {
cout << "Found it" << endl;
found = true;
}
}
In general, if things aren't working, you should be
- adding error checks if you're not checking for such things
- adding extra cout statements at key points in the code just to see if your intended code paths are actually happening. You can just as easily remove them again when you're happy it's working.
It ran, but the problem occurs that is it can store only 1 account and the new one create after that will overwrite the previous. Did I do something wrong here?
1 2 3 4 5 6 7 8 9 10 11 12
void Account::CreateAccount()
{
cout << "Nhap so tai khoan: ";
cin >> account_number_;
cout << "Nhap ten chu tai khoan: ";
cin.ignore();
cin.getline(name_, 100);
cout << "Nhap loai tai khoan: ";
cin >> type_;
cout << "So du ban dau: ";
cin >> balance_;
}