-1

I am new to c++ & don't know the basics all that well. pls help (sorry if the solution to this is already available, but I couldn't find any)

This is the Error I am getting:

expected primary-expression before ‘]’ token char CusName[50]=x[]; ^

For this code below:

#include <iostream>
using namespace std;

class BankAccount
{
    private:
    
    char CusName[50];
    char CusId[10];
    float accBalance, dep, witd;
    
    public:
    
    void setCusDetails(char x[], char n)
    {
        char CusName[50]=x[];
    }
};


int main()
{
    BankAccount customer1;

char cus1Name[50];
cin>>cus1Name;

customer1.setCusDetails(cus1Name, 50); 

    return 0;
}
  • Does this answer your question? [Is there a function to copy an array in C/C++?](https://stackoverflow.com/questions/16137953/is-there-a-function-to-copy-an-array-in-c-c) – Patrick Parker Apr 24 '21 at 07:20

2 Answers2

3

Your char array looks like a string. Try using std::string instead and prefer using const references for function parameters. If you want to use char arrays, and if your point was to copy a null-terminated string by value, then use functions like strncpy. Using std::string may be easier for you to hide the burden of memory allocation and discover the language step by step.

SR_
  • 874
  • 13
  • 31
-1

You can instead use string to input and pass values.

#include <iostream>
using namespace std;

class BankAccount
{
    private:
    
    string CusName; //CusName of type string
    char CusId[10];
    float accBalance, dep, witd;
    
    public:
    
    void setCusDetails(string str, char n) //parameter str of type string
    {
        CusName=str; //Assign it to the already declared 'CusName' variable.
    }
};


int main()
{
    BankAccount customer1;

string cus1Name;
cin>>cus1Name;

customer1.setCusDetails(cus1Name, 50); 

    return 0;
}
Prajwal Kulkarni
  • 1,480
  • 13
  • 22
  • I'm afraid it won't copy the string at all, and I guess it is what is expected. BTW you have a buffer overflow here. – SR_ Apr 24 '21 at 10:00
  • Hello, I've applied changes to the code, please check. As said, it's better to use a `string` data type which although is not primitive, but still can perform most of the needed operations. – Prajwal Kulkarni Apr 24 '21 at 10:10
  • That's better : no more buffer overflow and the string value is copied. A const ref string parameter would be a plus, but it works as it is. – SR_ Apr 24 '21 at 10:21