Help on random program.

I've been given a data file that lists Presidents Names, Last name First, Then First Name.

The object is to create a chart that looks like

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

The Code I have 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
50

#include<fstream>
#include<iostream>
#include<cstring>
using namespace std;

int main()
{
	struct Product 
	{
		int chronom;
		char pname[25];
		int next;
	};
	Product 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;
}

It spits out several errors...
error C2678: binary '!=' : no operator found which takes a left-hand operand of type 'main::Product' (or there is no acceptable conversion)

This is for the last for loop.

error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'main::Product' (or there is no acceptable conversion)

This is for the last out put command
cout<<....

error C2440: 'delete' : cannot convert from 'main::Product' to 'void *'

this is for the delete function in the last for loop.

My skills of C++ is limited, and I have no idea how to approach these errors without potentially screwing up my code.

Any help is appreciated.
Last edited on
closed account (3hM2Nwbp)
Eyes can't read code not in [code] tags. :-\
Personally you have your logic a little right and a little wrong. To me it has too many loops for the given task. It looks the print out look like we have an index counter a, an original position before sort, The last name with a comma before the first and the last number is place in historical president counts, or an array index.

if you don't know anything about operator overloading you need to evaluate your logic again. That is your first error. The Second one is related to something similar.

First step is make sure your getting your data to populate the array properly.

one question I have, are you trying to do a Linked list by chance from what I see in your code?

Another thing you do stuff like this.
Product pList[50]; This creates a static array
delete pList[x]; is expecting a something to be dynamically allocated, which it wasn't by the above line. It will be cleaned up at the exit of the program.
I can take the delete function out, however the other two errors are still appearing.

I'm at a loss of direction.
It seems like any correction I approach, it just further confuses me and just creates more issues, If anyone has any insight please help.

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
#include <fstream>
#include <cstring>
using namespace std;
voidmain ()
{
ifstream fin;
ofstream fout;
struct {int chronum; char pname[25];int next;} p list[50]; temp;
int n=0; 
int bottom;
int maxi;
int i;

fin.open("prez.dat");
fout.open("report.out");
fin>>plist[n].pname;

while(!fin.eof())
{
plist[n].chronum=n+1;
n++;
fin>>plist[n].pname;

for (bottom=n-1; bottom>0; bottom--)
{
   maxi=0;
   for (i=1; i<= bottom; i++)  
      if (  strcmp (plist[i].pname, plist[maxi].pname) > 0)    
          maxi=i;                                                                    
                                                                                                      
                                                                                                           
   temp=plist[maxi];
   plist[maxi]=plist[i];
   plist[i]=temp;   
}
}

return 0;
}


This was me trying to make it less complex, which only made more problems.. Any tips? Anything?
first thing I see is a confusion of loops now.

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
int nCount = 0;
fin.open("prez.dat");

// read in the data
while(!fin.eof())
{
       plist[nCount].chronum=nCount+1;
       nCount++;
       fin>>plist[nCount].pname;
} //end of while

//do the sort  
// sort algorithm needs work, appears to be a simple bubble sort..
for (bottom=nCount-1; bottom>=0; bottom--)
{
   maxi=0;
   for (i=1; i<= bottom; i++)  
      if (  strcmp (plist[i].pname, plist[maxi].pname) > 0)    
          maxi=i;                                                                    
                                                                                                      
                                                                                                           
   temp=plist[maxi];
   plist[maxi]=plist[i];
   plist[i]=temp;   
} // end of Sort loop

// produce report
fout.open("report.out");

for(nIndex = 0; nIndex < nCount; nIndex++)
{
      fout << plist[nIndex].chronum << pList[nIndex]........
}

fin.close();
fout.close();


I hope this helps you out to some extent end your confusion.
Last edited on
Topic archived. No new replies allowed.