Hello friends ,
I got an important coursework to submit . Let me explain, the coursework consist of a file .. which we must sort, search and we must event create a y-sort algorithm.
After much struggle, i was able to use the stringtokensier to input the data from the file as it was seperated by a tab and there are many lines in the file.
e.g Name Surname Address Age
Mark Twain Missippi 25
Tom Sawyer Missippi 15 and so on .
The problem now is that i am unable to distribute the name to the structures . please help me! I have not sleep for 2 days, i don't know how to do that .. after so much of hard work i dont want to fail this module so please help.
Here my code:
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
struct Person{
string name;
string surname;
string address;
string age;
};
int main(int argc, char *argv[])
{
ifstream fin;
fin.open("input.txt");
char buf[100];
string lineOfData;
string arraytemp[4];
int recordcounter=0;
Person person_name[recordcounter];
Person person_surname[recordcounter];
Person person_address[recordcounter];
Person person_age[recordcounter];
if (fin.is_open())
{
while(!fin.eof())
{
getline(fin,lineOfData, '\n');
lineOfData+="\n";
cout << "\n\n\n\n\n___________________\n\n";
cout << lineOfData << "\n\n";
cout << "size of line is: " << lineOfData.size() << "\n\n";
char delims[] = "\t";
for(int i=0; i<lineOfData.size(); i++)
{
buf[i] = lineOfData[i];
}
char *result = NULL;
result = strtok( buf, delims );
for(int counter=0; result != NULL; counter++ )
{
arraytemp[counter]=result;
cout << "\n\n result is: " << result << "\n";
result = strtok( NULL, delims );
}
}//for the while loop
fin.close(); //this is for the file
}
else cout << "Unable to open file";
for(int i=0; i<4; i++)
{
cout << "\n Checking array \t" << arraytemp[i];
//person_surname[recordcounter].surname=arraytemp[1]; <<<< PROBLEM IS HERE
//person_address[recordcounter].address=arraytemp[2];
//person_age[recordcounter].age=arraytemp[3];
//recordcounter++;
}
cout << arraytemp[0];
cout << person_name[0].name;
cout << "\n\n\n\n\n\n\n";
system("PAUSE");
return EXIT_SUCCESS;
}
Thanks in advance friends.
Thanks Daniweb, If daniweb was not here .. dont know what would happen, this is my first post but i got lots of answers here , Thanks.