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.
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++)
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.