4

I'm getting an error:

expression must be a modifiable lvalue at line, obj.name = ptr->name

I have tried to make obj an array type object like so

 for(int j=0 ;j<1;j++)
{
obj[j].id = ptr->id;
obj[j].balance= ptr->balance;
obj[j].name = ptr->name;   //still getting error here.
obj[j].nic = ptr->nic;
}
return(obj);
}

but that has not worked either.

if i comment the error out and pass just three remaining values it should work but i receive garbage values after the first out put.

here is the original code:

#include<iostream>
using namespace std;

struct bank
{
  int id, nic;
  float balance;
  char name[20];


};
bank search(bank* );

void main()

{

    bank data[2],mobj;
    for(int i=0;i<2;i++)
    {
    cout<<"enter name: ";
    cin>>data[i].name;
    cout<<"enter id: ";
    cin>>data[i].id;
    cout<<"enter balance : ";
    cin>>data[i].balance;
    cout<<"enter nic : ";
    cin>>data[i].nic;

    }


    mobj=search(data);

    cout <<"balance of customer no. "<<mobj.balance<<endl;
    cout<<"id is" <<mobj.id<<endl;
    cout<< "nic is"<<mobj.nic<<endl;



    system("pause");
}




bank search(bank *ptr)
{   
    int id;
    cout<<"enter value you want to serch"<<endl;
    cin>>id;
    bank obj;
for(int i=0 ; i<2 ;i++)
{
    if(ptr->id == id)
    {
        break;
    }
    ptr++;
}

obj.id = ptr->id;
obj.balance= ptr->balance;
obj.name = ptr->name;    //error in this line(obj must be modifiable value)
obj.nic = ptr->nic;

return(obj);
}

please help as you see fit!

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
Mustafa Mujahid
  • 65
  • 1
  • 2
  • 10
  • 1
    You can't do assignment on arrays. – T.C. Aug 19 '14 at 10:29
  • 1
    In C++ you should (almost) never use plain C-style arrays. Use `std::vector` or `std::array` depending on if you want dynamic or static size, or of course `std::string` if you are dealing with strings. – hyde Aug 19 '14 at 10:39
  • You again. Just a warning: you're fast approaching an automatic question ban due to this succession of low-quality "help me plz" questions. – Lightness Races in Orbit Aug 19 '14 at 11:25
  • 1
    @MustafaMujahid: It doesn't really matter whether it's your first day or your last. Or your 4000th. You should read the Help Centre and examine existing questions to find out what posts look like here. That you haven't been here very long is not much of an excuse. What "forum"? – Lightness Races in Orbit Aug 19 '14 at 11:29
  • This question _is_ much better than your previous ones, though! My concern is that posting so many questions in quick succession strongly implies that you are not spending enough of your _own_ time on your problems. It shines through in the lack of research effort, too. – Lightness Races in Orbit Aug 19 '14 at 11:30
  • You have other errors, by the way. What happens to your search if the id is not found? – CashCow Aug 19 '14 at 13:21
  • yeah i have that in mind ill just use an else statement fairly simple. – Mustafa Mujahid Aug 19 '14 at 19:08

3 Answers3

3

obj.name is an array of char. You cannot do assignments on arrays. So if you want to stick to arrays:

  1. see c++ array assignment of multiple values
  2. use strcpy(obj.name, ptr->name);

but I'd recommend to convert to std::string ... it's much easier to work with than arrays and it seems to me you plan to use obj.name as string. So got with the proper strings.

Community
  • 1
  • 1
Ferenc Deak
  • 34,348
  • 17
  • 99
  • 167
2

Your options are:

  • Have name as a std::string. Then it will work
  • use strcpy( obj.name, ptr->name );

If your code is C++ then the first has even more advantages that when you read it in with cin it will automatically ensure that the buffer is big enough for what the user enters.

Try entering a name of 20 or more characters now and see what happens. You will have undefined behaviour and your program may crash.

Those are the fixes anyway.

The reason your code does not work is that an array is not assignable. It is not an l-value. It is the start of a fixed array of bytes. I know it is confusing because you can use = to initialise it thus you can do:

char name[20] = "Frank Smith"; // legal. This is initialisation

but you can't do:

char name[20];
name = "Frank Smith"; // error. This is attempted assignment.

With std::string it works as string is a class in the standard library. (Actually the class is basic_string< char, char_traits<char>, allocator<char> > but assume it is a class for now). And it has methods that know how to assign to it, stream into it, etc. and manage the memory properly.

The only time the array in your struct is more useful is if you want to store it to disk or send it over a connection where you need "raw" data, or where you want to interface it with "C" or a scripting language that works with C structs.

CashCow
  • 30,981
  • 5
  • 61
  • 92
0

obj[j].name is an array. You can't copy an array like that.

lkanab
  • 924
  • 1
  • 7
  • 20