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.

line 29: recordcounter must be a const integer if it is to be used to set the array size as shown in lines 30-33. That means it must be a value > 0.

const int recordcount = 255;
Person person[recordcounter];

lines 30-33: delete all but one those arrays because you only need one of them. The struct contains all the information you need about one person -- you don't have to have an array for each attribute (name, address, etc). Example: Person person[recordcounter]; line 40: don't write the loop using eof() like that because it will cause problems. Instead write that loop like this:

while ( getline(fin, lineOfData) )
{

}

You could make your code even easier by using the >> operator if the names do not contains embedded spaces

Person person[recordcounter];
int i;
while(i < recordcounter &&  fin >> person[i].name )
{
    fin >> person[i].surname >> person[i].address
         >> person[i]. age;
  ++i;
}

Another approach is to use a vector (declared in the header file <vector> ) instead of array of Person objects. You don't have to set the max size of a vector like you do an array

vector<Person> person;
Person p;
while(fin >> p.name )
{
    fin >> p.surname >> p.address
         >> p. age;
  person.push_back(p);
}

Hello friends,
Wow . what a team , you reply to me so fast and i really happy , you are doing a good work.
First of all, Thank you for you answer ancient dragon .

The eof you told me to remove works, thanks for that.

I do as you said but it has not solve my main problem , I will not be able to used the >> operator as the names contains embedded spaces. The problem here is that the file consists of many lines .. meaning many names, surnames , addresses and ages.

The problem is how to store each data to a struct and how to access these data so that i will be able to sort them later on. I have done lot and lot of research work on struct and i am unable to manage with this, i really need your help guys. This is the beginning of my coursework and i would really appreciate if you help me to store these data in a struct.


You told me to use:

fin >> p.surname >> p.address >> p. age;

but as far as i know this will store the details for only one person, right ?? what about the others, hope you understand where i am stuck.

Thank you friends.
Bye, hope to have an answer soon.

>>but as far as i know this will store the details for only one person, right ?? what about the others, hope you understand where i am stuck.
Yes it will be for only one person -- BUT ...... you failed to read, or maybe comprehend, what the very next line is doing. It is adding that person to the array. Each time that push_back() line is executed it will add another name structure to the array of structures so that by the time the file is finished reading the array will contain a structure for each of the names.

>>I will not be able to used the >> operator as the names contains embedded spaces.
You mean there can be spaces in the first name or spaces in the last name? None of the data you posted is of that format. Only spaces between the names, not within the names.

Hello friends,
Thanks for replying, you are right ,i have not understood the function of the push_back().

Another thing i have not understand, as you are using the fin operator, the program is reading directly from the file, you are not using the strtok(string tokeniser) then, isn't it ?

The data in the file are
e.g

Name          Surname       Address       Age
Azagen Jo    Mootoo         Mauritius     18
Tom            Sawyer        Missippi        15
Mark Paul    Twain           Missippi        25

and so on .

Maybe i forgot to tell someting , the data is seperated by a tab, that's why i used the strtok.

Eg. Azagen(space)Jo(tab)Mootoo(tab)Mauritius(tab)18(newline)
Tom(tab)Sawyer(tab).....

This is only a sample data by in reality the file (.txt) contains 500lines and about 20 columns of data, with consists of characters, integers and floating point.

Here i used a simple text file, to understand the logic and to see if the code is working.

Programmers always said that DIVIDE AND CONQUER is the best way to solve a problem.

You see, this is not an easy coursework for a first year student but still i am trying hard. I would be pleased if you could help me with this part, its only the beginning of the coursework.

I re-write the code, but its still not working.
I am cout the data ..(i am using the cout for testing, to see if the data has been stored in the struct).

#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
//#include "StringTokenizer.h"


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];

        const int recordcounter = 255;
        vector<Person> person;
        Person p;


if (fin.is_open())
{



while(getline(fin, lineOfData)) // do not write the loop using eof() like that because it will cause problems
{              
        getline(fin,lineOfData, 'n');
        lineOfData+="n";




     int i;

      fin >> p.name >> p.surname >> p.address >> p. age;

     i++;






        cout <<  "nnnnn___________________nn";
        cout << lineOfData << "nn";
        cout << "Size of line is: " << lineOfData.size() << "nn";

        char delims[] = "t";
        for(int i=0; i<lineOfData.size(); i++)
        {

         buf[i] = lineOfData[i];  

        }



        char *result = NULL;
        result = strtok( buf, delims );
        //for(int i=0; result != NULL; i++ )
        //{ 

       while(fin >> p.name)    


              fin >> p.name >> p.surname >> p.address >> p. age;              
              person.push_back(p);
       }



        //cout << "nn Result is: " << result << "n";  
        //result = strtok( NULL, delims ); 







 fin.close();   

}
    else cout << "Unable to open file";        


    for(int i=0; i<4; i++)
    {




    cout << "Testing : " << i << "  ";
    cout << person[i].name << person[i].surname << person[i].address << person[i]. age;
    cout << "n";



    }








    cout << "nnnnnnn";
    system("PAUSE");
    return EXIT_SUCCESS;
}

Thank you Ancient Dragon.

Edited 3 Years Ago by mike_2000_17: Fixed formatting

>>Mark Paul Twain Missippi 25
That line is not in the format you specified. It contains middle name (Paul). So do you store the middle name in the firstname or lastname, or just ignore it altogether?

>>This is only a sample data by in reality the file (.txt) contains 500lines and ~20 columns of data, with consists of characters, integers and floating point.
Then you have a lot more work ahead of you because the program must read all the columns in order to get to the next line. Maybe it would be more efficient to read one whole line at a time into one std::string then split it up with stringstream.

Post one entire line from that file -- load it into notepad, copy a line or two into the clipboard, then paste it here. Also an explaination of what each column means.

Hello friends,
Just check the text file i upload and you will understand the problem, the name, surname, address and age was just a small file i created in order to test the program.

My problem here is that i must used this text file to sort, search and create a new algorithm (known as the Yop-sort). I think i will be able to manage with sorting and so on .. but i am unable to store these values in a struct, please help me friend .. its really important.


You said something about std::string, i came across that when i was doing my research by i was unable to implement it. If you can show me how to store these values in a struct(or any other way that will help me to sort, search later on) would be a great thing for me .

Thank you.

Attachments
World Rank	Institution	Region	Regional Rank	Country	National Rank	Score on Alumni	Score on Award	Score on HiCi	Score on N&S	Score on SCI	Score on Size	Total Score

1	Harvard Univ	Americas	1	USA	1	100	100	100	100	100	72.4	100

2	Univ Cambridge	Europe	1	UK	1	99.8	93.4	53.3	56.6	70.9	66.9	73.6

3	Stanford Univ	Americas	2	USA	2	41.1	72.2	88.5	70.9	72.3	65	73.4

4	Univ California - Berkeley	Americas	3	USA	3	71.8	76	69.4	73.9	72.2	52.7	72.8

5	Massachusetts Inst Tech (MIT)	Americas	4	USA	4	74	80.6	66.7	65.8	64.3	53	70.1

6	California Inst Tech	Americas	5	USA	5	59.2	68.6	59.8	65.8	52.5	100	67.1

7	Columbia Univ	Americas	6	USA	6	79.4	60.6	56.1	54.2	69.5	45.4	62.3

8	Princeton Univ	Americas	7	USA	7	63.4	76.8	60.9	48.7	48.5	59.1	60.9

9	Univ Chicago	Americas	8	USA	8	75.6	81.9	50.3	44.7	56.4	42.2	60.1

10	Univ Oxford	Europe	2	UK	2	64.3	59.1	48.4	55.6	68.4	53.2	59.7

11	Yale Univ	Americas	9	USA	9	52.1	44.5	60.3	57.2	63.9	49.3	56.9

12	Cornell Univ	Americas	10	USA	10	46.5	52.4	55	48.8	66.3	39.8	54.6

13	Univ California - San Diego	Americas	11	USA	11	17.7	34.7	59.8	56.5	64.5	46.6	51

14	Univ California - Los Angeles	Americas	12	USA	12	27.3	32.8	56.7	50.1	75.6	34.3	50.6

15	Univ Pennsylvania	Americas	13	USA	13	35.5	35.1	56.7	42.9	71.8	39.1	50.2

16	Univ Wisconsin - Madison	Americas	14	USA	14	43	36.3	52.1	46.3	68.7	29	49.2

17	Univ Washington - Seattle	Americas	15	USA	15	28.8	32.4	53.9	47.1	73.8	27.2	48.4

18	Univ California - San Francisco	Americas	16	USA	16	0	37.6	55.6	57.9	58.8	45.2	47.8

19	Johns Hopkins Univ	Americas	17	USA	17	51.4	28.3	41.6	52.2	67.7	24.9	46.9

20	Tokyo Univ	Asia/Pac	1	Japan	1	36	14.4	38.5	52.1	86.5	34.7	46.7

21	Univ Michigan - Ann Arbor	Americas	18	USA	18	43	0	61.9	43	76.5	30.9	44.9

22	Kyoto Univ	Asia/Pac	2	Japan	2	39.7	34.1	34.2	37	72.3	31.1	43.8

23	Imperial Coll London	Europe	3	UK	3	20.8	38.1	40.8	38.2	64.6	40.3	43.7

24	Univ Toronto	Americas	19	Canada	1	28.1	19.7	39.3	38.9	76.7	41.9	43.1

25	Univ Illinois - Urbana Champaign	Americas	20	USA	19	41.6	37.4	44.4	34.1	58	26	42.8

26	Univ Coll London	Europe	4	UK	4	30.7	32.9	37.7	41.5	60.5	38.8	42.6

27	Swiss Fed Inst Tech - Zurich	Europe	5	Switzerland	1	40.2	37	35.1	41.1	43.4	52.4	41.7

28	Washington Univ - St. Louis	Americas	21	USA	20	25.1	26.6	38.5	46.5	53.9	39.9	40.7

29	New York Univ	Americas	22	USA	21	33.8	25	43	35.3	55.4	26.3	38.8

30	Rockefeller Univ	Americas	23	USA	22	22.6	59.8	28.3	44.1	24	35.9	38.2

31	Northwestern Univ	Americas	24	USA	23	21.7	19.3	44.4	33.8	57.6	36.2	37.9

32	Duke Univ	Americas	25	USA	24	20.8	0	47.1	45.3	60.8	38.9	37.7

32	Univ Minnesota - Twin Cities	Americas	25	USA	24	36	0	49.7	35.2	68.4	23.8	37.7

34	Univ California - Santa Barbara	Americas	27	USA	26	0	36	42.3	39	44.1	35.8	36.9

35	Univ Colorado - Boulder	Americas	28	USA	27	16.6	29.8	40.8	36.6	46.3	29.5	36.3

36	Univ Texas - Austin	Americas	29	USA	28	21.7	17.1	49.1	30	54.8	21.7	35.5

37	Univ British Columbia	Americas	30	Canada	2	20.8	19.3	32.4	32.5	60.4	33.9	35.4

38	Univ Texas Southwestern Med Center	Americas	31	USA	29	24.3	33.9	31.4	38.2	37.9	31	34.8

39	Pennsylvania State Univ - Univ Park	Americas	32	USA	30	14	0	45.8	37.9	59.9	24	33.4

39	Vanderbilt Univ	Americas	32	USA	30	12.5	30.2	34.2	24.5	49.2	35.6	33.4

41	Univ California - Davis	Americas	34	USA	32	0	0	46.5	34.5	64	29.8	32.9

41	Univ Utrecht	Europe	6	Netherlands	1	30.7	21.4	27.2	27.3	55.7	25.9	32.9

43	Rutgers State Univ - New Brunswick	Americas	35	USA	33	15.4	20.4	36.9	32.9	47.1	24.1	32.3

43	Univ Pittsburgh - Pittsburgh	Americas	35	USA	33	25.1	0	40.1	25.9	64.3	28.2	32.3

45	Karolinska Inst Stockholm	Europe	7	Sweden	1	30.7	27.8	33.3	19.7	47.3	25.1	32.1

46	Univ Paris 06	Europe	8	France	1	35.7	23.9	23.6	24.2	51.2	30	32

47	Univ California - Irvine	Americas	37	USA	35	0	30	32.4	28.5	48.2	31.1	31.8

47	Univ Edinburgh	Europe	9	UK	5	22.6	17.1	26.1	35.8	49.4	29.9	31.8

47	Univ Maryland - Coll Park	Americas	37	USA	35	25.9	0	40.8	33.6	54.6	25.6	31.8

50	Univ Southern California	Americas	39	USA	37	0	27.3	37.7	23.6	52.8	25.8	31.7

51	Univ Munich	Europe	10	Germany	1	37.1	21.1	15.7	30.4	51.9	30	31.4

52	Tech Univ Munich	Europe	11	Germany	2	43	24.1	24.8	20.7	46.5	29.2	31.3

53	Univ Manchester	Europe	12	UK	6	27.3	19.3	23.6	22.6	57.3	30.4	31.2

54	Carnegie Mellon Univ	Americas	40	USA	38	30.7	33.5	32.4	14.7	38.3	31.4	30.8

55	Univ North Carolina - Chapel Hill	Americas	41	USA	39	12.5	0	35.1	32.8	59.5	27.3	30.3

56	Australian Natl Univ	Asia/Pac	3	Australia	1	17.7	12.9	36.9	29	45.1	27.8	30.2

57	Univ Copenhagen	Europe	13	Denmark	1	30.7	24.7	23.6	22.8	45.7	27.7	30

57	Univ Florida	Americas	42	USA	40	15.4	0	35.1	25	65.2	25.8	30

57	Univ Zurich	Europe	13	Switzerland	2	12.5	27.3	19.2	30.3	47.2	30.6	30

60	Uppsala Univ	Europe	15	Sweden	2	25.9	32.9	11.1	28.7	49.1	21.6	29.9

61	Univ Paris 11	Europe	16	France	2	33.2	34.2	13.6	19.6	44.9	27.9	29.4

62	Osaka Univ	Asia/Pac	4	Japan	3	12.5	0	23.6	31.1	66.8	29.2	29.3

63	Ohio State Univ - Columbus	Americas	43	USA	41	17.7	0	40.8	21.5	61.2	19.5	29.2

64	Univ Bristol	Europe	17	UK	7	10.9	18.2	30.4	24.5	47.5	27.4	28.8

65	Univ Rochester	Americas	44	USA	42	33.2	9.1	27.2	25.3	43	36.1	28.6

65	Univ Sheffield	Europe	18	U

That is a UNICODE formatted text file which can not be read with standard fstream objects (that I know of). UNICODE files are not normal text files. The first two bytes are binary -1 and -2. So you first have to read the first two bytes to determine if it is in UNICODE format or not. The file you posted indicates it is UNICODE, and a hex dump of the file also confirmed that.

How did you generate that file? did your instructor give it to you for the class assignment or did you generate it yourself ?

Here is one way to read that file and read it into a structure. VC++ 2008 Express compier.

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
using namespace std;

struct data
{
   int WorldRank;
   string Institution;
   string Region;
   string RegionalRank;
   string Country;
   string NationalRank;
   int ScoreOnAlumni;
   int ScoreOnAward;
   int ScoreOnHiCi;
   int ScoreOnNS;
   int ScoreOnSCI;
   int ScoreOnSize;
   int TotalScore;
};

ifstream& getaline(ifstream& in, char* line, size_t maxlinesize)
// read one line from a UNICODE formatted file and saves it into
// a standard ascii character array.
{
    char wc[sizeof(wchar_t)];
    size_t i = 0;
    while(i < maxlinesize && in.read(wc, sizeof(wc)) )
    {
        if( wc[0] == '\n')
            break;
        line[i++] = wc[0];
    }
    line[i] = 0;
    return in;
}

int main()
{
    vector<data> lst;
    ifstream in("..\\arw.txt", ios::binary);
    if( in.is_open() )
    {
        char line[255] = {0};
        in.read(line, 2);
        if(line[0] == -1 && line[1] == -2)
        {
            // unicode line
            // get a line
            getaline(in, line, sizeof(line)); // ignore the first line of titles
            while( getaline(in, line, sizeof(line)) )
            {
                // this is a tab deliminated file which means each column is deliminated
                // with a tab character.
                data dt;
                stringstream stream(line);
                string word;
                getline(stream,word,'\t');
                dt.WorldRank = atol(word.c_str());
                getline(stream,dt.Institution,'\t');
                getline(stream,dt.Region,'\t');
                getline(stream,dt.RegionalRank,'\t');
                getline(stream,dt.Country,'\t');
                getline(stream,dt.NationalRank,'\t');
                // etc. etc for the rest of the structure members

            }              
        }
        else
        {
            // ascii file
        }
        

    }

}

Hello,
Wow , i am amazed , are you a robot or what (lol) !! You write this code so fast, you are a genius !! oufff .. I am currently downloading vc++, i am using dev compiler at this time, i will try to compile it on dev and let you know, hope this work, i will never forget what you have done for me.

Thank you , i will let know if i got any problems.

Again Thank You Ancient Dragon and Dani Web Team.

Bye.

Hello,

I run the code .. there are not errors , but one thing i dont understand where is the

main int main(int argc, char *argv[])
{


and the system("PAUSE"),

I try to put the MAIN and the SYSTEM PAUSE and I compiled it on Dev C++ and it showing errors, i have never used VC++. Does this code run only on VC++ ? and one last thing friend , how will i know that the struct has stored on the values , let said for world rank , you have used some reserved words that i have never seen before .. Could you just give me an example, please.

Just write the line of code to access and to output all the value for world rank.

Thank You.

see line 41 of the code I posted. If you need the arguments just simply add them on that line.

I don't use system("PAUSE"). But you can add it anywhere you want.

>>how will i know that the struct has stored on the values
Add print stateuemts cout << "rank = " << dt.WorldRank

Comments
Very kind, helpful and patient!

Hello friends ,
Thank you for the answer Ancient Dragon, sorry not to reply earlier , i didn't get much time, too many coursework , i just submitted part a ludo game in C (ouff) now i got time to continue with this sorting problem which i must submit on Monday 14th April.

I still got a problem, the code you send me works fine, but i find something .. i want to be sure that i wont have problem later on .. cause i am limited in time, on this internet this is the only forum which is replying me.. and thanks again Ancient Dragon, you have help me a lot.

The problem is that when i put the cout << (eg below) to see if the struct has stored the data ... it works ... but
.
.
.

cout << "\n\nrank = " << dt.WorldRank;
cout << "\n\n\nInstitution = " << dt.Institution;

.
.
.
(cont)

I observed the output of the file on screen and i found that columns which contains no data in the text file takes the value of rank

e.g the last few lines of the output were

ScoreOnHiCi = 401
ScoreOnSize = 401
TotalScore = 401

But in the text file, these part were blank.. my question is that, how will i be able to skip these numbers as i will deal with sorting, searching, hope you understand my point and one last question , do you think this coursework is easy or too hard, just to have an idea if i will get enough time to complete this coursework ??

Thanking you in advance.
Bye

Hello friends,
I really need your help .. yeah , your code is cool , i started to understand it when i play a bit with it. Can you please give me an answer for the question above .. i got another problem .. it seem to be simple but i am unable to modify your code.

You declare the WorldRank as an integer but this is not the case as after rank 100 the rank becames

101-152
101-152
101-152
101-152
101-152
101-152
101-152
101-152
101-152
101-152

something like this, which makes WorldRank not an integer .. i try to modify the code but it through an error .

can you please change the first line for me ..

stringstream stream(line);
string word;
getline(stream,word,'\t');
dt.WorldRank = atol(word.c_str());
getline(stream,dt.Institution,'\t');

its really important , i got only 3 days left to work on the yop sort .. i have manage to make the new algorithm but without being able to sort the file , i wont be able to implement the yop sort, please help me.

Finally, the ScoreAward is in float, when i change it to float .. meaning "float ScoreOnAward", its still ouputting the value in integer, maybe i must change something in this

dt.ScoreOnAward = atol(word.c_str());

but what ? i try to play with it but still no result. Please help me.


Thank you friends .

Hello Ancient Dragon,
Please do not consider post #13, as its working, i finally find the solution, but for #14, i got still have some problems, please help.

Bye

This article has been dead for over six months. Start a new discussion instead.