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

时间:2015-09-23 01:05:56

标签: c++ variables text-files fstream string-comparison

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

随着更多信息的出现,我会每隔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;
} 

输出是: 阅读文件...... JackieSamTomBillMaryPaulZevBarbJohnJohn排在第一位。 而萨姆排在最后。

2 个答案:

答案 0 :(得分:1)

我建议您使用数组然后使用算法排序功能

Array是一种数据结构,用于在程序运行时保存数据。

因此我们可以将这些数据从文件保存到该数组。数组的名称是 dataFromFile ,可以最多保存9个字符串值。 所以如果您的文件中有更多名称,只需更新数组的大小或使用矢量

  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>进行排序,然后在数组上使用sort方法,该方法称为 dataFromFile

sort(begin(dataFromFile),end(dataFromFile));

然后重新显示你拥有的数组

for(int j= 0 ;j < 9;j++)
{
    // after sorting
   cout<<dataFromFile[j] <<endl;
}

答案 1 :(得分:0)

不使用数组,是最佳解决方案,if语句中存在逻辑错误。

首先,字符串被初始化为空,因此空字符串总是被排序为first_In_Line。 first_In_Line需要在while循环的第一次迭代中分配一个值。

接下来,在while循环的第四次迭代中,变量变得不合理地分配,并且&#34; Sam&#34;通过while循环的其余部分在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... \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;
}