Problems for calling function

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_;
}

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
int main()
{
	Menu();
	return 0;
}

void Menu()
{

	char menu_option;
	do
	{
		system("cls");
		cout << "\n\n\n\tMenu";
		cout << "\n\n\t1. Tao tai khoan";//create account
		cout << "\n\n\t2. Nop tien";//top up money
		cout << "\n\n\t3. Rut tien";//withdraw
		cout << "\n\n\t4. Tra cuu tai khoan";//search account
		cout << "\n\n\t5. Danh sach tai khoan";//listofAccount
		cout << "\n\n\t6. Xoa tai khoan";//Delete account
		cout << "\n\n\t7. Sua tai khoan";//Change account 
		cout << "\n\n\t8. Thoat";//exit 
		cout << "\n\n\tChon menu (so tu 1 den 8): ";
		cin >> menu_option;
		system("cls");
		switch (menu_option)
		{
		case '1':
			CreateAccount();
			break;
		case '2':
			cout << "\n\n\tChon so tai khoan: ";
			DepositOrWithdraw(account_number, 1);
			break;
		case '3':
			cout << "\n\n\tChon tai khoan: ";
			DepositOrWithdraw(account_number, 2);
			break;
		case '4':
			cout << "\n\n\tNhap so tai khoan: ";
			SearchAndPrintAccount(account_number);
			break;
		case '5':
			PrintAllAccountInFormat();
			break;
		case '6':
			DeleteAccount(account_number);
			break;
		case '7':
			cout << "\n\n\tNhap so tai khoan: ";
			SearchAndModifyAccount(account_number);
			break;
		case '8':
			Eject();
			break;
		default:cout << "\a";
		}
		cin.ignore();
		cin.get();
	} while (menu_option != '8');

}


1
2
3
4
5
6
7
8
9
10
11
void CreateAccount()
{
	Account account;
	ofstream out_file;
	out_file.open("account.dat", std::ios_base::binary);
	if (out_file.is_open())
	{
		out_file.write(reinterpret_cast<char*> (&account),sizeof(Account));
	}
	out_file.close();
}


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
Last edited on
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.

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
int main()
{
	Menu();
	return 0;
}

void Menu()
{
	Account myAccount;  // Your instance of an Account
	char menu_option;
	do
	{
		system("cls");
		cout << "\n\n\n\tMenu";
		cout << "\n\n\t1. Tao tai khoan";//create account
		cout << "\n\n\t2. Nop tien";//top up money
		cout << "\n\n\t3. Rut tien";//withdraw
		cout << "\n\n\t4. Tra cuu tai khoan";//search account
		cout << "\n\n\t5. Danh sach tai khoan";//listofAccount
		cout << "\n\n\t6. Xoa tai khoan";//Delete account
		cout << "\n\n\t7. Sua tai khoan";//Change account 
		cout << "\n\n\t8. Thoat";//exit 
		cout << "\n\n\tChon menu (so tu 1 den 8): ";
		cin >> menu_option;
		system("cls");
		switch (menu_option)
		{
		case '1':
			myAccount.CreateAccount();  // reads from user
			SaveToFile(myAccount);  // saves to file
			break;
		case '2':
			cout << "\n\n\tChon so tai khoan: ";
			DepositOrWithdraw(account_number, 1);
			break;
		case '3':
			cout << "\n\n\tChon tai khoan: ";
			DepositOrWithdraw(account_number, 2);
			break;
		case '4':
			cout << "\n\n\tNhap so tai khoan: ";
			SearchAndPrintAccount(account_number);
			break;
		case '5':
			PrintAllAccountInFormat();
			break;
		case '6':
			DeleteAccount(account_number);
			break;
		case '7':
			cout << "\n\n\tNhap so tai khoan: ";
			SearchAndModifyAccount(account_number);
			break;
		case '8':
			Eject();
			break;
		default:cout << "\a";
		}
		cin.ignore();
		cin.get();
	} while (menu_option != '8');
}

void SaveToFile(Account &account)
{
	ofstream out_file;
	out_file.open("account.dat", std::ios_base::binary);
	if (out_file.is_open())
	{
		out_file.write(reinterpret_cast<char*> (&account),sizeof(Account));
	}
	out_file.close();
}

#salem c

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?

Thanks a lot!
Last edited on
You know it's the same thing with a different name right?
1
2
3
4
5
6
7
8
9
10
void SaveToFile(Account &account)
{
	ofstream out_file;
	out_file.open("account.dat", std::ios_base::binary);
	if (out_file.is_open())
	{
		out_file.write(reinterpret_cast<char*> (&account),sizeof(Account));
	}
	out_file.close();
}


What you wrote
1
2
3
4
5
6
7
8
9
10
11
void CreateAccount()
{
	Account account;
	ofstream out_file;
	out_file.open("account.dat", std::ios_base::binary);
	if (out_file.is_open())
	{
		out_file.write(reinterpret_cast<char*> (&account),sizeof(Account));
	}
	out_file.close();
}




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.

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
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;


in the Menu()
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
void Menu()
{
	int account_number;// it has this one !
	Account myAccount;
	char menu_option;
	do
	{
		system("cls");
		cout << "\n\n\n\tMenu";
		cout << "\n\n\t1. Tao tai khoan";//create account
		cout << "\n\n\t2. Nop tien";//top up money
		cout << "\n\n\t3. Rut tien";//withdraw
		cout << "\n\n\t4. Tra cuu tai khoan";//search account
		cout << "\n\n\t5. Danh sach tai khoan";//listofAccount
		cout << "\n\n\t6. Xoa tai khoan";//Delete account
		cout << "\n\n\t7. Sua tai khoan";//Change account 
		cout << "\n\n\t8. Thoat";//exit 
		cout << "\n\n\tChon menu (so tu 1 den 8): ";
		cin >> menu_option;
		system("cls");
		switch (menu_option)
		{
		case '1':
			myAccount.CreateAccount();  // reads from user
			SaveToFile(myAccount);  // saves to file
			break;
		case '2':
			cout << "\n\n\tChon so tai khoan: ";
			DepositOrWithdraw(myAccount.GetAccountNumber(), 1);
			break;
		case '3':
			cout << "\n\n\tChon tai khoan: ";
			DepositOrWithdraw(myAccount.GetAccountNumber(), 2);
			break;
		case '4':
			cout << "\n\n\tNhap so tai khoan: ";
			SearchAndPrintAccount(myAccount.GetAccountNumber());
			break;
		case '5':
			PrintAllAccountInFormat();
			break;
		case '6':
			DeleteAccount(myAccount.GetAccountNumber());
			break;
		case '7':
			cout << "\n\n\tNhap so tai khoan: ";
			SearchAndModifyAccount(myAccount.GetAccountNumber());
			break;
		case '8':
			Eject();
			break;
		default:cout << "\a";
		}
		cin.ignore();
		cin.get();
	} while (menu_option != '8');

}


The variable account_number does not have any value to set up. That what I think is the problem here! One more time, apologize to you for my mistake!

So because I don't know how to set the account_number so I try to get their number by usingmyAccount.GetAccountNumber()for each case? Is it possible?
Last edited on
1
2
3
4
5
		case '4':
			cout << "\n\n\tNhap so tai khoan: ";
			cin >> account_number;
			SearchAndPrintAccount(account_number);
			break;


and

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
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";
}

#salem c
Thanks a lot. That is the one that I don't know. Make some mistakes in logic thinking
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_;
}


1
2
3
4
5
6
7
8
9
10
void SaveToFile(Account &account)
{
	ofstream out_file;
	out_file.open("account.dat", std::ios_base::binary);
	if (out_file.is_open())
	{
		out_file.write(reinterpret_cast<char*> (&account),sizeof(Account));
	}
	out_file.close();
}


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
void Menu()
{
	int account_number;
	Account myAccount;
	char menu_option;
	do
	{
		system("cls");
		cout << "\n\n\n\tMenu";
		cout << "\n\n\t1. Tao tai khoan";//create account
		cout << "\n\n\t2. Nop tien";//top up money
		cout << "\n\n\t3. Rut tien";//withdraw
		cout << "\n\n\t4. Tra cuu tai khoan";//search account
		cout << "\n\n\t5. Danh sach tai khoan";//listofAccount
		cout << "\n\n\t6. Xoa tai khoan";//Delete account
		cout << "\n\n\t7. Sua tai khoan";//Change account 
		cout << "\n\n\t8. Thoat";//exit 
		cout << "\n\n\tChon menu (so tu 1 den 8): ";
		cin >> menu_option;
		system("cls");
		switch (menu_option)
		{
		case '1':
			myAccount.CreateAccount();  // reads from user
			SaveToFile(myAccount);  // saves to file
			break;
		case '2':
			cout << "\n\n\tChon so tai khoan: ";
			cin >> account_number;
			DepositOrWithdraw(account_number, 1);
			break;
		case '3':
			cout << "\n\n\tChon tai khoan: ";
			cin >> account_number;
			DepositOrWithdraw(account_number, 2);
			break;
		case '4':
			cout << "\n\n\tNhap so tai khoan: ";
			cin >> account_number;
			SearchAndPrintAccount(account_number);
			break;
		case '5':
			PrintAllAccountInFormat();
			break;
		case '6':
			cin >> account_number;
			DeleteAccount(account_number);
			break;
		case '7':
			cout << "\n\n\tNhap so tai khoan: ";
			cin >> account_number;
			SearchAndModifyAccount(account_number);
			break;
		case '8':
			Eject();
			break;
		default:cout << "\a";
		}
		cin.ignore();
		cin.get();
	} while (menu_option != '8');

}

1
2
3
4
5
void Account::PrintAccountInFormat() const
{
	cout << left << setw(11) << account_number_ << setw(19) << name_;
	cout << left << setw(8) << type_ << setw(14) << balance_ << endl;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void PrintAllAccountInFormat()
{
	Account account;
	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");
		return;
	}
	cout << "\n\n\t\tDanh sach tai khoan:\n\n";
	cout << "====================================================\n";
	cout << "So TK      Chu TK             Loai    So du\n";
	cout << "====================================================\n";
	while (in_file.read(reinterpret_cast<char *> (&account), sizeof(Account)))
	{
		account.PrintAccountInFormat();
	}
	in_file.close();
}

kinda work but just only for an account.
Last edited on
Perhaps open the file for appending, whenever you save a new account.
1
2
3
void SaveToFile(Account &account)
{
	ofstream out_file("account.dat", std::ios_base::binary | std::ios_base::app);

http://www.cplusplus.com/reference/fstream/ofstream/open/
Topic archived. No new replies allowed.