Fill an array of struct door

What am i doing wrong? I keep getting errors.
I included errors in code.
Im supposed to read 2 arrays and put them in a file, then read the file and 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
54
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string.h>
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];
	int length, width;
	int i=0;
	arrayFile >> length >> width; //error says no operators match these operands
	while (!arrayFile.eof())
	{
		tab[i].length=length; //error: expression must be a modifiable lvalue
		tab[i].width=width; //error: expression must be a modifiable lvalue
		i++;
		door >> length >> width; //error says no operators match these operands
	}

	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();
}
line 35 is supposed to say
 
arrayFile >> length >> width;
Line 27: These names conflict with the arrays defined at lines 13-14.
This is the cause of all subsequent errors.

Line 29: You're trying to read into arrays (lines 13-14). Not a supported operation.

Line 32-33: You're trying to assign arrays (lines 13-14) to ints.

Line 35: You're trying to output arrays.
How can i fix line 32-33? I have fixed the rest.
Since I can't see your fixes, I can't tell you what to do.
Last edited on
This is what i have now, i still have errors on 32-33
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
54
//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 tabs[10];
	arrayFile >> length[10] >> width[10];
	int i=0;
	while (!arrayFile.eof())
		{
			tabs[i].length = length;
			tabs[i].width = width;
			i++;
			arrayFile >> length[10] >> width[10];
		}

	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();
}
Not sure why struct door is two arrays - only need two ints there.

Line 28 - by your line 14/15 declarations, there is no length[10] or width[10], and while maybe you intend to use one of your array elements to hold the input from arrrayFile, I don't know why. In lines 32 and 33 you are trying to set tabs[i] to the value of a variable that doesn't match its declaration. There is no length, only length[], no width, only width[].

And why couldn't you just

arrayFile >> tabs[i].length >> tabs[i].width

in a do/while loop anyway?
Last edited on
Line 28,35: As PCrumley48 pointed out, length[10] and width[10] are out of bounds references. length and width have only elements [0] to [9]. It's poor style to read data into a pre-populated array. Lines 14-15 should really be declared as const int, indicating you do not intended to modify the values.

Lines 32-33: As I pointed out before, you can't assign an array (length, width) to an int.

ezmesori wrote:
How can i fix line 32-33?

I think you're confusing your length and width arrays with int values.

Try this for lines 27-36:
27
28
29
30
31
32
33
34
35
36
37
  door tabs[10];
  int l,w;   // instead of length and width
  int i = 0;

  arrayFile >> l >> w; 
  while (!arrayFile.eof())
  {  tabs[i].length = l;
      tabs[i].width = w;
      i++;
      arrayFile >> l >> w; 
  }

Topic archived. No new replies allowed.