Writing an enum in a struct to a struct array

So I have a project due tomorrow (yes, this is homework, but only a clarification/technical question that I can't find on Google) and, as the title says, I'm trying to write from a file to a struct array and store them as structs.

In addition, I don't know how to access the "height" and "width" ints in the struct.
Here is my code. The line denoted with asterix's is the part I'm inquiring about.


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
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

struct art {
	string artist;
	string title;
	enum medium {a, o, p, f, w };
	struct size {
		int height;
		int width;
	};
	enum room {m, g, u, n, s, e, b };
	int price;
} ArtWork, GalleryCollection[500];

int main()
{
	ifstream input;
	input.open("art.dat");
	while (input >> GalleryCollection[])
            {
	int j=0;
        input >> GalleryCollection[j].artist >> GalleryCollection[j].title >>

****************************************************************************
                 GalleryCollection[j].medium >>

                 GalleryCollection[j].ArtHeight.height >>

                 GalleryCollection[j].ArtWidth.width >>

                 GalleryCollection[j].room >>
****************************************************************************
                 GalleryCollection[j].price >> endl;
		j++;
	    }
	input.close();

	system("pause");
	return 0;
}



Since "medium" and "room" are of type enum, it won't let me pass it to a struct array. I have no idea how to access the enum type(s) within the "art" struct, nor how to access the objects inside the "size" struct within the "art" struct. Any thoughts?
Last edited on
medium and room are enum declarations. They do not allocate a member variable in the struct type art.

Move the enum declarations out of the struct declaration and add an enum member variable in their place.

1
2
3
4
5
6
7
8
9
10
11
12
13
enum medium_t {a, o, p, f, w };
enum room_t {m, g, u, n, s, e, b };
struct art 
{  string artist;
   string title;
   medium_t medium;
   struct size 
   {  int height;
       int width;
   };
   room_t  room;
   int price;
} ArtWork, GalleryCollection[500];


PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.


Oh okay, that makes much more sense.

One more thing though: how do I call the declarations "height" and "width" inside struct "size" within struct "art?" Would it be something like Artwork.size.height?
You don't "call" declarations.

Line 7 (my previous post): You have the same problem here with your struct as you did with your enums. size is a struct declaration. It is not a member variable. Sorry about not catching that in my previous post. Same solution applies.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
enum medium_t {a, o, p, f, w };
enum room_t {m, g, u, n, s, e, b };
struct size_t
{  int height;
    int width;
};
struct art 
{  string artist;
   string title;
   medium_t medium;
   size_t  size;
   room_t  room;
   int price;
} ArtWork, GalleryCollection[500];


Would it be something like Artwork.size.height?

Yes.

Last edited on
Thanks so much for the feedback!

Unfortunately, when you fix one problem, two more pop up. I'll just paste the whole program because this new problem is a little hard to explain out of context.

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 <string>
#include <fstream>
using namespace std;

enum mediumt {a, o, p, f, w};
enum roomt {m, g, u, n, s, e, b};
struct sizet {
	int height;
	int width;
};
struct art {
	string artist;
	string title;
	sizet size;
	mediumt medium;
	roomt room;
	int price;
} ArtWork, GalleryCollection[500];

int main()
{
	ifstream input;
	input.open("art.dat");
	

	{
		int j=0;
		input >>
			GalleryCollection[j].artist >>
			GalleryCollection[j].title >>
*******************************************************
			GalleryCollection[j].medium >>
*******************************************************
			GalleryCollection[j].size.height >>
			GalleryCollection[j].size.width >>
*******************************************************
			GalleryCollection[j].room >>
*******************************************************
			GalleryCollection[j].price;

		cout << "The name of the artist is " << GalleryCollection[0].artist << endl;
		cout << "The name of the painting is " << GalleryCollection[0].title << endl;
		cout << "The material the painting was made out of is " << GalleryCollection[0].medium << endl;
		cout << "The height of the painting is " << GalleryCollection[0].size.height << endl;
		cout << "The width of the painting is " << GalleryCollection[0].size.width << endl;
		cout << "The room where the painting is hung is the " << GalleryCollection[0].room << " room" << endl;
		cout << "This painting is worth $" << GalleryCollection[0].price << endl;
	}
	input.close();

	system("pause");
	return 0;
}



A bit of background: I have a txt file called art.dat which contains "TedSmith RedRock o 10 12 n 12500." That's <artist name> <painting title> <material painted with> <height> <width> <room stored in> <price>.

For a reason unknown to me, the asterix'd lines of code "GalleryCollection[j].medium" and GalleryCollection[j].room" are giving me errors, saying that "no operator matches these operands ">>" on line 32 (GalleryCollection[j].title >>). However, if I take those two lines out, the program runs fine. How do I fix it?
Last edited on
mediumt and roomt are not build in types and therefore the operators >> and << do not work.

One way to fix it is to read the input into an int and convert it.
1
2
3
int medium;
input >> medium;
GalleryCollection[j].medium = (mediumt) medium;


Alternatively you could write your own insertion operator, but this is more complicated.
"TedSmith RedRock o 10 12 n 12500."

n and o are char data.

Thomas1965's suggestion to read into an int is not going to give you the results you expect. Attempting to read a 'o' or 'n' into an int is going to cause the >> operation to fail.

AbstractionAnon is right. Somehow I failed to notice that they are chars. So it might better to read them in a char and convert it to mediumt and roomt types.
Topic archived. No new replies allowed.