最小/最大逻辑错误和文件读取错误

Min/max Logic and file read error

本文关键字:文件 读取 取错误 错误 最小      更新时间:2023-10-16

读取文件和最小/最大逻辑

随着信息的增加,我将每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;
}