Struct error

Dec 8, 2009 at 3:03am
The database that i need to use is in chronological order by presidents names.The chronum is what number president the particular individual was.The pointer(next prez) filed links a president to his successor.THe problem I am having is it is telling me that"error C2228: left of '.pname' must have class/struct/union"or"error C2228: left of '.chronom' must have class/struct/union"Thanks for the help

so I need to output something like this:

location chronum presidents name next prez
0 2 Adams,john 22
1 6 Adams,jons_quincy 21
2 21 Arthur,Chester 7
. . .... .
. . .... .
. . .... .
42 1 Washington,George 0
43 28 Wilson,Woodrow 16

this is what I got so far:
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
#include<fstream>
#include<iostream>
#include<cstring>
using namespace std;

int main()
{
	struct
	{
		int chronom;
		char pname[25];
		int next;
	};
	char plist[50];
	int temp;
	int i;
	int rightmost;
	int imax;
	ifstream fin;
	ofstream fout;

	fin.open("prez.dat");
	fout.open("output.dat");
	int n=0;
		fin>>plist[n].pname;
	while(!fin.eof())
	{
		plist[n].chronom = n+1;
		n++;
		fin>>plist[n].pname;
	}
	for(rightmost=n-1;rightmost>0;rightmost--)
	{
		imax=0;
		for(i=1;i<=rightmost;i++)
			if(strcmp(plist [i].pname,plist[imax].pname)>0)
				imax=i;
		temp = plist[imax];
		plist[imax]=plist[rightmost];
		plist[rightmost]=temp;
	}

	cout<<"location\tchronum\tpresident's name\tnext prez"<<endl<<endl;
	for (int i = 0; plist[i] != NULL && i < 50; i++) {
        cout << n << " " << plist[i].chronom << " " << plist[n].pname << " " << plist[rightmost] << endl;
        delete plist[i];
    }
	return 0;
}
Dec 8, 2009 at 3:12am
1
2
3
4
5
6
	struct
	{
		int chronom;
		char pname[25];
		int next;
	};


Don't you want to name your struct?
Dec 8, 2009 at 4:20am
like
1
2
3
4
5
6
struct A
	{
		int chronom;
		char pname[25];
		int next;
	};


??
Dec 8, 2009 at 4:25am
Yes. Now that it has a name, you probably want to use it somewhere...
Dec 8, 2009 at 5:09am
But how it still gives me the same errors
Dec 8, 2009 at 7:10am
I think I am doing something wrong here but I dont know what

1
2
3
4
5
6
7
8
9
10
11
12
struct product
	{
		int chronom;
		char pname[25];
		int next;
	}plist[50];
	int temp;
	int i;
	int rightmost;
	int imax;
	ifstream fin;
	ofstream fout;
Dec 8, 2009 at 1:47pm
This was your initial code:

1
2
3
4
5
6
7
8
9
10
	struct
	{
		int chronom;
		char pname[25];
		int next;
	};
	char plist[50];
	int temp;
	int i;
	int rightmost;


Afterwards, you altered it like this:

1
2
3
4
5
6
7
8
9
10
11
12
struct product
	{
		int chronom;
		char pname[25];
		int next;
	}plist[50];
	int temp;
	int i;
	int rightmost;
	int imax;
	ifstream fin;
	ofstream fout;


So, you took out the type for plist[50]. Put char plist[50];, as it were.
Last edited on Dec 8, 2009 at 1:48pm
Dec 8, 2009 at 2:00pm
In C++ it is best to define the struct, then define the variable of the struct type separately. Why you did is common in C (and therefore legal in C++), but probably inappropriate for C++. That's because in C++, struct is a type-definition just like class.

1
2
struct Product {...};
Product plist[50];


or better:

 
vector<Product> plist;

Dec 8, 2009 at 4:57pm
How about afterwards it says "error C2440: '=' : cannot convert from 'main::product' to 'int'"what should I do now?
Dec 8, 2009 at 5:04pm
the error is in lines
1
2
3
temp = plist[imax];
		plist[imax]=plist[rightmost];
		plist[rightmost]=temp;
Dec 9, 2009 at 2:40am
What type did you declare temp as? And what type is plist?
Topic archived. No new replies allowed.