Problem: assigning strings to structures member

Hello all,

I am trying to assign two movies to the structure MovieData then display those two movies. The problem I am having is with lines 29, 30, 34, and 35. My debugger is saying this "Error E2277 Movie Data.cpp 29: Lvalue required in function main()." What seems to be the problem?
I am using borland 5.5.1 for Win32 with jGrasp as the text editor.
Thanks in advance.

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
//Movie Data base with structs
#include <iostream>
using namespace std;

//create structure MovieData
//MovieData - Title, Director, Year Released, Running Time(in min.)

const int SIZE = 81; //Character Array Size

//Structure for movie data base
struct MovieData
{
	char Title[SIZE];
	char Director[SIZE];
	int Year;
	int RunTime;
};

//function prototype
void showMovie(MovieData);

int main()
{
	//two movie data
	//store values in their members
	MovieData Airplane;
	MovieData KungFu;
	
	Airplane.Title = "Airplane";
	Airplane.Director = "Zucker";
	Airplane.Year = 1980;
	Airplane.RunTime = 88;
	
	KungFu.Title = "Kung Fu Hustle";
	KungFu.Director = "Stephen Chow";
	KungFu.Year = 2004;
	KungFu.RunTime = 95;
	
	//pass each one to a function that displays the info. in a format 
	showMovie(Airplane);
	showMovie(KungFu);	
	
	char c;	//define char variable c
	cin >> c;	//read a char from keyboard
	return 0;
}

void showMovie(MovieData &)
{
  //code yet to be written

}
Last edited on
Whenever you declare an array as
int numbers[ 10 ];
you get the variable numbers, which we can treat as if it were a pointer (since using its value gives you the address of the first element in the array), but the compiler knows it isn't really a normal pointer, because it addresses actual memory somewhere in the computer.

If I were allowed to say:
1
2
int* returns_more_numbers();
numbers = returns_more_numbers();

this would cause a problem: we have the address of more numbers, but we have permanently forgotten the address of the original array of numbers.

In order to prevent that, C and C++ make array variables const. In otherwords, the array itself may be mutable, but the variable that addresses it is not.
int* const numeros = numbers;
Now we have created a new variable ('numeros') which is immutable (you can't change its value) just like 'numbers'. But, I can still mess with the contents of the array:
1
2
numeros[ 5 ] = -7;
numbers[ 7 ] = 42;

There is one important distinction though. The compiler knows the dimensions of 'numbers' but does not know the dimensions of 'numeros'.

You are trying to modify an array variable. Both Title and Director are array variables addressing existing arrays of characters.

So, what you need to do is copy the data from the array "Airplane" to the array Airplane.Title.

You can use the strcpy() function in <cstring>
1
2
3
#include <cstring>
...
strcpy( Airplane.Title, "Airplane" );


Or you can use the built in char_traits copy function:
 
char_traits <char> ::copy( Airplane.Title, "Airplane", 9 );


Or you can use STL algorithms to copy it:
1
2
3
#include <algorithm>
...
copy_n( "Airplane", 9, Airplane.Title );

(Hmm, come to think of it, copy_n() is relatively new. Your compiler might not have it...)

Hope this helps.
Thanks a bunch for the help, Duoas. C++ get pretty tricky for me and its nice to know there is a forum like this where there are knowledgeable people willing to help.
Topic archived. No new replies allowed.