最小/最大逻辑错误和文件读取错误
Min/max Logic and file read error
读取文件和最小/最大逻辑
随着信息的增加,我将每30分钟更新一次我的问题、语句和代码,这样我就不会比一些人回答得快了。
我的问题是,如何将程序设置为一次读取一个名称而不是将名称连接起来?
该文件为。txt文件,如下所示:Jackie Sam Tom Bill Mary Paul Zev Barb John
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
// File stream objects
ifstream inputFile;
inputFile.open("LineUp.txt");
// Non-user variables
string first_In_Line = "",
last_In_Line = "",
previous_Name = "",
next_name = "";
if (inputFile)
{
// Display message to user
cout << "Reading file... n";
while (inputFile >> next_name)
{
cout << next_name;
if (next_name > last_In_Line)
{
first_In_Line = last_In_Line;
last_In_Line = next_name;
}
else if (next_name < first_In_Line)
{
last_In_Line = first_In_Line;
first_In_Line = next_name;
}
// This else clause should only apply to the first iteration
else
{
first_In_Line = next_name;
}
}
//Close the file
inputFile.close();
// Display first in line and last in line
cout << first_In_Line << " is first in line." << endl;
cout << "And " << last_In_Line << " is last in line." << endl;
}
else
{
// Display error message.
cout << "Error opening the file.n";
}
return 0;
}
输出是:阅读文件…约翰排在第一位。
我建议你使用数组然后使用算法sort函数
数组是一种数据结构,用于在程序运行时保存数据。
因此,我们可以将这些数据从文件保存到该数组中。数组的名称是dataFromFile,可以保存最多9个字符串值。所以如果你的文件中有更多的名字只需更新数组的大小或者使用vector ifstream file("dataToRead.txt");
string dataFromFile[9];
string line;
int index = 0;
if(!file)
{
cout<<"cannot find this file" <<endl;
}
else
{
if(file.is_open())
{
while (getline(file,line))
{
dataFromFile[index] = line;
index++;
}
file.close();
}
}
然后使用循环
显示数组内的内容 for(int j=0;j<9;j++)
{
// to do display
cout<<dataFromFile[j] <<endl;
}
NOW只对#include <algorithm>
进行排序,然后使用数组上的排序方法dataFromFile
sort(begin(dataFromFile),end(dataFromFile));
然后重新显示到数组
中for(int j= 0 ;j < 9;j++)
{
// after sorting
cout<<dataFromFile[j] <<endl;
}
如果不使用数组,这是最好的解决方案,在if语句中有一个逻辑错误。
首先,字符串初始化为空,因此空字符串总是按照first_In_Line排序。first_In_Line需要在while循环的第一次迭代时赋值。
接下来,到while循环的第四次迭代时,变量的赋值变得不合逻辑,并且在while循环的其余部分中,"Sam"在first_In_Line和last_In_Line之间来回传递。
我是这样解决这个问题的:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
// File stream objects
ifstream inputFile;
inputFile.open("LineUp.txt");
// Non-user variables
string first_In_Line = "",
last_In_Line = "",
next_name = "";
if (inputFile)
{
// Display message to user
cout << "Reading file... nn";
while (inputFile >> next_name)
{
cout << next_name << endl; // list the names
if (last_In_Line == first_In_Line)
{
first_In_Line = next_name;
}
else if (next_name > last_In_Line)
{
last_In_Line = next_name;
}
else if (next_name < first_In_Line)
{
first_In_Line = next_name;
}
}
//Close the file
inputFile.close();
// Display first in line and last in line
cout << endl << first_In_Line << " is first in line." << endl;
cout << "And " << last_In_Line << " is last in line." << endl;
}
else
{
// Display error message.
cout << "Error opening the file.n";
}
return 0;
}
相关文章:
- 正在将csv文件读取为双精度矢量
- 将从格式化文本文件读取的文本数据存储到链表
- C++ 读取文件读取文件不正确
- 使用对象文件读取三角形数据网格
- 如何减慢从 BVH 文件读取的 opengl 动画?
- 从文件读取,并写入函数C++中的数组
- 从对象C++中的文件读取内容时出现分段错误
- 从文件读取并通过矢量输出到控制台
- 尝试将字符串从文件读取到无符号字符向量中
- 如何将文本文件读取到二维数组中并以 c++ 打印
- 简化文件读取的函数
- 如何将逗号分隔的文件读取为 2D int 数组?
- 在 c++ 中将数据从文件读取到队列中
- 'std::wstring_convert'尽可能多地转换(从 UTF8 文件读取块)
- C++,从文件读取到结构,然后读取到向量(结构被推入向量太多次,而不仅仅是一次)
- 文件读取C++包括空格键
- 从文件读取时未将值推回矢量
- 从文件 C++ 读取时的无限循环
- 从同一文件读取和写入?
- 从.txt文件读取到C++中的双精度向量