来自 c++ 文本文件的无组织输出

Unorganized output from a text file in c++

本文关键字:无组织 输出 文件 c++ 文本 来自      更新时间:2023-10-16

我正在用 c++ 编写一个程序,其中我从文本文件中读取数据,然后使用排序算法对它们进行排序。我不允许使用文件操作来读取输入。我正在使用"for 循环"从文件中读取所有数据。我遇到的部分是当我尝试打印文件的最后一部分时,它变得混乱且杂乱无章。我的文本文件大约有 220 行,我的程序读取它很好,直到文件的最后一部分是我的函数。

打印后我应该得到这个:

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

但我得到这个:

bsort2015penaltiesdecr
bsort20102015scrimmage_playsincr
2013yds_per_gamemaxbsort20112013first_per_gamedecr
2010third_attmedianbsort20112013pts_per_gamedecr
bsort20132015top_per_gameincr
bsort2014team_nameincr

我正在使用if语句来打印这部分,这是我代码的一部分:

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);
    }

任何帮助将不胜感激。

您忘记在输出中添加空格。 如果你想在两个输出之间出现一个空间,你必须把它放在那里。 你可以用

cout << some_variable << " ";

每当您有两个或更多呼叫要cout而之间没有cin时,您需要添加某种分隔符。 在

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);
}

您可以输出yearfieldorder,没有任何分离。 如果您希望它们全部在一行上,则可以将其更改为

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