Expression must be a modifiable lvalue

I keep getting this error. Can someone please tell me what im doing wrong. Thanks! I am trying to fill an array of struct door.
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
53
 //Read array. Put them in a file. Read the file and fill an array of struct door
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std; 
void readatof(int x[], int y[], int n);
struct door
{
	int length[10], width[10];
};
int main()
{
	int length[]={10,20,30,40,50,60,70,80,90,100};
	int width[]={5,10,15,20,25,30,35,40,45,50};

	readatof(length, width, 10);

	fstream arrayFile;
	arrayFile.open("C://Users//Esmeralda//Desktop//arrays.txt",ios::in);
	if (arrayFile.fail())
	{
		cout << "Error openning file" << endl;
		exit(0);
	}

	door tab[10];
	arrayFile >> length[10] >> width[10];
	for(int i=0;i<10;i++)
		{
			tab[i].length = length; //error: Expression must be a modifiable lvalue
			tab[i].width = width;  //error: Expression must be a modifiable lvalue
			arrayFile >> length[10] >> width[10];
		}

	arrayFile.close();
	system("pause");

	return 0;

}	
void readatof(int x[], int y[], int n)
{
	fstream arrayFile;
	arrayFile.open("C://Users//Esmeralda//Desktop//arrays.txt",ios::out);
	if (arrayFile.fail()){exit(0);}
	for (int i=0;i<n;i++)
	{
		arrayFile << x[i] << "\t" << y[i] << endl;
	}

	arrayFile.close();
}
Last edited on
i'm assuming you want to copy the values of length and width individually.

try changing

1
2
//length to length[i];
//width to  width[i]; 
It still gives me the same error
here is a simple program let me know if this is what you want to do?

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
// ConsoleApplication4.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std; 
void readatof(int x[], int y[], int n);


struct door
{
	int length, width;
};

int _tmain(int argc, _TCHAR* argv[])
{
	int length[]={10,20,30,40,50,60,70,80,90,100};
	int width[]={5,10,15,20,25,30,35,40,45,50};

	door tab[10];

	for(int i=0;i<10;i++)
	{
			tab[i].length = length[i]; //error: Expression must be a modifiable lvalue
			tab[i].width =  width[i];  //error: Expression must be a modifiable lvalue
			
	}

	
	for(int i=0;i<10;i++)
	{
			std::cout << "length" << i << " " <<  tab[i].length << endl;
			std::cout << "width" <<  i << " " <<  tab[i].width << endl;  //error: Expression must be a modifiable lvalue			
	}
	
	int x;
	cin >> x;

	return 0;
}
Thanks! That helped a lot!
buuuut.. now it says stack around the variable "width" was corrupted after it runs. I cant figure out why?
if you paste a copy of your txt file. i will try to replicate your problem.
Topic archived. No new replies allowed.