来自 c++ 文本文件的无组织输出
Unorganized output from a text file in c++
我正在用 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);
}
您可以输出year
、field
和order
,没有任何分离。 如果您希望它们全部在一行上,则可以将其更改为
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);
}
相关文章:
- 来自无符号字符缓冲区的虚拟输出
- 使用OpenMP获得完整、无损坏的输出需要什么
- 预购遍历树时无输出
- CMD:程序.exe 1< "A:/input.txt"产生无输出
- 为什么printf和cout为此无符号int提供了不同的输出
- 如何将 Openh264 解码帧无延迟输出格式转换为 OpenCV 矩阵
- 输出类似于 FORTRAN 的无格式C++
- 如何从doxygen输出中排除类的无证件成员函数/变量
- 为什么尽管变量是无符号整数,但该程序仍以 -25 给出输出
- C++ "Hello world"显示无输出
- 如何在球形空间中找到一组点,在无组织的点云中,特定半径在一个点附近
- 无"hello world "输出 (C++)
- 获得程序输出(无错误),但程序.exe在使用 graphics.h 时不起作用
- 这里发生了什么,还有输出是什么?(无线索)
- C 在代码中初始化字符串变量时无控制台输出
- 无符号长长 - 奇怪的输出
- C 中的输出程序无明显的原因是白色的
- 在 OpenCV 中播放视频:无错误无输出
- 无输出和随机字符串数组
- 来自 c++ 文本文件的无组织输出