mercredi 3 février 2016

Unorganized output from a text file in c++

I am writing a program in c++ in which I read the data from a text file and then will sort them using sorting algorithm. I am not allow to use file operations to read the input. I am using "for loop" to read all data from the file. The part I am having issue is when I try to print the last part of the file it gets messed up and unorganized. My text file is about 220 lines, and my program is reading it fine until the last part of the file which are my functions.

I should get this after printing:

bsort 2015 penalties decr
bsort 2011 yds_per_game incr
bsort range 2010 2015 scrimmage_plays incr
bsort 2012 top_per_game incr
bfind 2013 yds_per_game max
bfind 2015 fum average
bsort range 2011 2013 first_per_game decr
bfind 2014 yds_per_play min
.
.
.

but I get this:

bsort2015penaltiesdecr
bsort20102015scrimmage_playsincr
2013yds_per_gamemaxbsort20112013first_per_gamedecr
2010third_attmedianbsort20112013pts_per_gamedecr
bsort20132015top_per_gameincr
bsort2014team_nameincr

I am using if statements to print this part and here is a part of my code:

cin >> c; //Read number of commands
cout << c << endl; //Print the number of commands
cin.ignore();

char** commands =  new char*[c];  //Creat a new array type char to read commands, read the complete line
//Read commands
for(int i = 0; i<c; i++)
{
    commands[i] = new char[256]; //Read the array of the lines
}

string next; //Declare next type string
 for(int i = 0; i <= c - 1; i++){
    cin>>commands[i]; //read the command name
    if(strcmp(commands[i], "bsort") == 0) // check wether it's bsort or not
    {
        cout<<commands[i];
        cin>>next; // if it is bort, we get the next string
        if(next.compare("range") == 0)
        {
            int start_year  , end_year;
            cin>>start_year;
            cout<<start_year;
            cin>>end_year;
            cout<<end_year;
            string field;
            cin>>field;
            cout<<field;
            string order;
            cin>>order;
            cout<<order<<endl;
            cin.ignore();
            bsort(stats,y, start_year, end_year, field, order);
        }
        else
        {
            int year = atoi( next.c_str() );
            cout<<year;
            string field, order;
            cin>>field;
            cin>>order;
            cout<<field;
            cout<<order<<endl;
            cin.ignore();
            bsort(stats, y, year, field, order);
            //Call bsort function by passing only 1 year in the bsort function
        }

    }
   else if(strcmp(commands[i], "bfind") == 0)
    {
        int year;
        cin >> year;
        string field, order;
        cin>>field;
        cin>>order;
        cout<<year;
        cout<<field;
        cout<<order;
        cin.ignore();
        bfind(stats, y, year, field, order);

    }

Any help is greatly appreciated.

Aucun commentaire:

Enregistrer un commentaire