Lvalue required....

1
2
3
4
5
6
7
8
9
10
11
12
13
struct student
{
 char first[30];
 char last[20];
} stud_1;

main()
{
 stud_1.first = "James";
 strcpy(stud_1.first, "James");
 cout << stud_1.first;
 getch();
}


when i compiled this, it returned me an error = lvalue required. I tried to change the char to string but still no luck. Seriously this char thing pissed me off..please help me. Thanks in advance!!!

-cplusx2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <string>

using namespace std;


struct student
{
 string first;
 string second;
} stud_1;

main()
{
 stud_1.first = "James";
  cout << stud_1.first;
}
What Moschops says. Don't use C-type strings if you can avoid it, because they are very confusing for beginners.

If you're curious about what the error exactly means: arrays are basically pointers. stud_1.first is a pointer to stud_1.first[0], i.e. the first element of stud_1.first. That means the line
stud_1.first = "James";
translates to "change the address of stud_1.first to "James"". The RHV doesn't make much sense, but the compiler will do his best to translate the string literal "James" into an address. It'll be useless, but okay. The LHS, however, is unmodifiable. In essence, you're saying "move stud_1.first to a different position in memory".
Last edited on
Damn, more errors!!! If i change it to string, i got these errors:

type name expected;
declaration is missing;
..
and so on.....

maybe my c++ compiler has bugs?
Maybe you forgot #include <string> ? Not string.h, not cstring, just string.

Your compiler is almost never the problem [unless when using Dev-C++, but even that one will function properly for this piece of code].

[edit]

Or you missed using namespace std; (or using std::string;), in which case it doesn't recognize "string".
Last edited on
hmm, i used Borland C++, if that helps anything. Anyway, so i put the string header and compile it...success but the cmd prompt doesn't show anything when i run it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream.h>
#include <string>
#include <conio.h>

struct student
{
 string first;
 string second;
} stud_1;

main()
{
 stud_1.first = "James";
  cout << stud_1.first;
 getch();
} 
There's no 'h' in iostream...

Is it not showing anything, or closing immediately?
hmm, i used Borland C++, if that helps anything.


I think if you're learning, you should use a C++ compiler that conforms to the C++ standard. There are many free IDEs and compilers that do so.
There's no 'h' in iostream...

Is it not showing anything, or closing immediately?


it doesn't show anything but blank output....

I think if you're learning, you should use a C++ compiler that conforms to the C++ standard. There are many free IDEs and compilers that do so.


i thought borland c++ is similar with the rest of the compilers...anyway, my course used it so i have no choice but to stick with it. So, is there any ways to solve this matter? I downloaded some notes regarding on structures and they used char instead of string...!'m a bit confused at first since using char caused me error! so, either the notes wrong or me myself just sucks at programming..
Just copy-paste this exact snippet. Don't add (or remove) .h's, don't remove "using namespace std" and don't touch any of the code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <string>
#include <conio.h>

using namespace std;

struct student
{
    string first;
    string second;
} stud_1;

int main()
{
    stud_1.first = "James";
    cout << "Name of stud_1: ";
    cout << stud_1.first;
    getch();
    return 0;
} 

Does it show anything when you run it?
Ok, done compiling...showed some buildtime msgs...when i run it, it only shows the line 16 till i pressed any key, then the name showed up....so line 16 >> press any key >> "James" shows up
line 16 >> press any key >> "James" shows up
It is likely caused by the buffering of cout.
Makes your line 17 cout << stud_1.first << flush; or cout << stud_1.first << endl;
after too many trial-and-error, i've finally solved this problem...it's simple, just use:

strcpy()

Yeah, gonna remember that from now on. Anyway, thanks to all who help me, I really appreciated it!

-cplusx2
Last edited on
Seriously, look into getting a modern compiler. If your compiler cannot handle

stud_1.first = "James";

it is horribly, horribly broken.
1
2
3
4
5
Seriously, look into getting a modern compiler. If your compiler cannot handle

stud_1.first = "James";

it is horribly, horribly broken. 


Agreed. Too bad my edu course system still stuck with the old compiler. Will take note about it tho!
Last edited on
Topic archived. No new replies allowed.