Compare user input (char array) with string from file.

Hello everyone. I have a problem to search - [by using customer's name] - for a customer's bank account details from a binary .dat file. Here are some relevant codes that I think you guys can help me out.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
char acc_name[20];
char accounts_menuact;
cout << "1 -> CREATE ACCOUNT\n";
cout << "2 -> DEPOSIT AMOUNT\n";
cout << "3 -> WITHDRAW AMOUNT\n";
cout << "4 -> BALANCE ENQUIRY\n";
cout << "Select an option: ";
cin >> accounts_menuact;
    else if (accounts_menuact == '2'||accounts_menuact == '3')
    {
        cout << "Customer Name: ";
        cin >> acc_name;
        s3.deposit_withdraw (acc_name[20],accounts_menuact);
    }

Function deposit_withdraw accepts 2 arguments.
I'm aware you can't pass array as a parameter in a function,thus you're seeing this post.
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
deposit_withdraw (char& acc_name,char& accounts_menuact)
 {
    Savings s3;
    fstream inout_stream;
    inout_stream.open ("account details.dat", ios::binary|ios::out|ios::in);
    if (!inout_stream)
    {
        cout << "Unable to open file ! Press Any Key . . .";
        return;
    }
    while ((!inout_stream.eof ()) && (find_record == false))
    {   
        inout_stream.read(reinterpret_cast<char *>(&s3), sizeof(Savings));
        if (acc_name == s3.input_accname ())    //this condition doesn't work
        {
            system ("cls");
            s3.layout ();
            if (accounts_menuact == '2')
            {
                cout << "DEPOSIT MONEY INTO ACCOUNT";
                cout << "Enter amount of money to be deposited : RM ";
                cin >> amount;
                s3.deposit_money (amount);
            }
        else if (accounts_menuact == '3')
            {
                cout<<"WITHDRAW MONEY FROM ACCOUNT ";
	        cout<<"Enter amount of money to be withdraw : RM ";
	        cin>>amount; 
             //the coding continues...
            }
      

Call input_accname function to validate customer's identity.
1
2
3
4
char input_accname ()
{
    return acc_name[20];
}

I thought of using string to store acc_name as its variable, but after I've done some research, I found out line 13 doesn't accept string data type.So I'm open to any suggestions or help from you guys. Thanks.
I'm aware you can't pass array as a parameter in a function,thus you're seeing this post.

Would it help to know that you can still effectively pass an array into a function by just passing the name of the array, which passes the address of its first element? Check out this article, particularly "Arrays as Parameters": http://www.cplusplus.com/doc/tutorial/arrays/

You should be able to make the function header look like
 
deposit_withdraw (char acc_name[], char& accounts_menuact)

And then when calling the function just use
 
s3.deposit_withdraw (acc_name, accounts_menuact);

There is a code snippet in that section of the article with explanations of how this all works. Hope this helps.
Check out this article, particularly "Arrays as Parameters"

http://www.cplusplus.com/doc/tutorial/arrays/


I've already read the tutorial beforehand, but it doesn't work though. The problem with this method is:

- the console prints out the remaining unused spaces in the array
- if it works I don't know how to apply it in this condition :

if (acc_name == s3.input_accname ())
Last edited on
I solved it using the code below:

if (strcmp(name, s3.acc_name) == 0)

Thanks for the suggestion though.
Topic archived. No new replies allowed.