Min/max逻辑和文件读取错误
Min/max Logic and file read error
正在读取文件和Min/max逻辑。
随着更多信息的到来,我将每 30 分钟更新一次我的问题、陈述和代码,这样我的编辑速度不会比某些人的回答速度快。
我的问题是,如何将程序设置为一次读取一个名字而不是连接这些名字?
该文件是一个 .txt 文件,内容如下:
Jackie Sam Tom Bill Mary Paul Zev 巴布约翰
#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;
}
输出为:
读取文件...
JackieSamTomBillMaryPaulZevBarbJohnJohn 排在第一位。
Sam 排在最后。
我给你的建议是使用array 然后使用算法sort function
数组是一种数据结构,用于在程序运行时保存数据运行。
因此我们可以将这些数据从文件保存到该数组。数组的名称是 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;
}
现在只对它们进行排序 #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 循环的第四次迭代中,变量的赋值变得不合逻辑,"Sam" 在 first_In_Line 和 last_In_Line 之间来回传递while 循环。
我是这样解决这个问题的:
#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... \n\n";
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;
}
正在读取文件和Min/max逻辑。
随着更多信息的到来,我将每 30 分钟更新一次我的问题、陈述和代码,这样我的编辑速度不会比某些人的回答速度快。
我的问题是,如何将程序设置为一次读取一个名字而不是连接这些名字?
该文件是一个 .txt 文件,内容如下: Jackie Sam Tom Bill Mary Paul Zev 巴布约翰
#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;
}
输出为: 读取文件... JackieSamTomBillMaryPaulZevBarbJohnJohn 排在第一位。 Sam 排在最后。
我给你的建议是使用array 然后使用算法sort function
数组是一种数据结构,用于在程序运行时保存数据运行。
因此我们可以将这些数据从文件保存到该数组。数组的名称是 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;
}
现在只对它们进行排序 #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 循环的第四次迭代中,变量的赋值变得不合逻辑,"Sam" 在 first_In_Line 和 last_In_Line 之间来回传递while 循环。
我是这样解决这个问题的:
#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... \n\n";
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;
}