Can some one help me with this.I have a project and I have a file with the list off all the presidents from Washington to Obama it looks like
WASHINGTON,GEORGE
ADAMS,JOHN
JEFFERSON,THOMAS
MADISON,JAMES
MONROE,JAMES
ADAMS,JOHN_QUINCY
and so on.
What I need to do is make a list that shows the name of the president. Which number they were in order and which president is next. for example
0 2 adams,John 22
the 22 so it can show which president is next by that first column and and the first one is just in order from 0-50. This is what I have
#include<fstream>
#include<iostream>
#include<cstring>
usingnamespace std;
int main()
{
struct president
{
int location;
int chronom;
char pname[25];
int next;
};
president *plist[50] = { NULL };
president *temp;
int i;
int rightmost;
int imax;
ifstream fin;
ofstream fout;
fin.open("prez.dat");
fout.open("list.txt");
int n=0;
while(!fin.eof())
{
plist[n] = new president();
fin>>plist[n]->location;
fin>>plist[n]->chronom;
fin>>plist[n]->pname;
fin>>plist[n]->next;
n++;
}
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;
}
fout<<"location"<<"\t"<<"chronum"<<"\t"<<"president's name"<<"\t"<<"next prez"<<endl<<endl;
for (int i = 0; plist[i] != NULL && i < 50; i++) {
fout << i<<plist[i]->location << " " << plist[i]->chronom << " " << plist[i]->pname << " " << plist[i]->next << endl;
}
return 0;
}
can some one help me with the output? im not getting anything. When I debug it, it shows nothing being taken in. I am using cstyle string. We can change if need be.
You haven't read a line in from the file. You need to do something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
fin.open("prez.dat");
fout.open("list.txt");
int n=0;
string line;
while(!fin.eof())
{
std::getline (fin, line);
plist[n] = new president();
// parse the line variable by whatever it is delimited by
// and then place these into the plist[n] structure
n++;
}