Someone Please take a look And see If you can Help me find whats wrong

My code will not compile. How do i get the string to work correctly? I am used exactly what my professer put on the board. This program should be compiling.
I am not sure how to apply the C++ strings.

#include <iomanip>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{

struct{int chronum;string pname;int next;}//makes structure box with C++ style string
plist[50],temp;//give the number of colmns
int n=0,end,maxindex,i;
ifstream fin;
ofstream fout;
fin.open("pres.dat");
fout.open("hw7.dat");
fin>>plist[n].pname;//inputs president name into list
while(!fin.eof())
{
plist[n].chronum=n+1;//assigns chronum to presidents as inputed.
n++;//tracks current president
fin>>plist[n].pname;
}
for(end=n-1;end>0;end--)
{
maxindex=0;
for(i=1;i<=end;i++)

if(plist[i].pname>plist[maxindex].pname)
maxindex=i;
temp=plist[maxindex];
plist[maxindex]=plist[end];
plist[end]=temp;
}
//cout.setf(ios::fixed);
//cout.setf(ios::showpoint);
//cout.precision(2);
//cout<<plist[i].chronum<<plist[i].pname<<endl;
return 0;
}
Last edited on
Please use code tags: [code]Your code[/code]

Instead of just telling that there are errors post the error.

I don't see any obvious error. Maybe you compiled it as a c file? It must be c++ (-> *.cpp)
Struct declaration is wrong.

It needs a name and is missing a terminator at the end.

Might not be the overall cause, but it's the first one I've found and that about as far as I'm willing to look right now.

As coder said, please use tags.
iHutch105 wrote:
Struct declaration is wrong.

No it's not. He has created instances of that struct on the line below.
@iHutch105
The struct declaration isn't wrong (just somewhat obfuscated). you can use unnamed structs
Ah yes, my bad, I didn't see the plist and temp instances. That'll be why there's no terminator at the end of that line. Ignore above comment.
Are you sure this is a compilation error and not a runtime error?
I don't see any reason why it wouldn't compile, but there are some potential problems with the logic.

1
2
3
4
5
6
7
	fin>>plist[n].pname;//inputs president name into list
	while(!fin.eof())
	{
		plist[n].chronum=n+1;//assigns chronum to presidents as inputed.
		n++;//tracks current president
		fin>>plist[n].pname;
	}


Should probably be:

1
2
3
4
5
	while ( n<50 && fin >> plist[n].pname )
	{
		plist[n].chronum = n+1 ;
		++n ;
	}



And for(i=1;i<=end;i++) should probably have i starting at 0 and not 1.

Declaring/defining variables at the point of first use should be preferred when it's possible. The struct member next serves no purpose in this code.


Topic archived. No new replies allowed.