【问题标题】:Min/max Logic and file read error最小/最大逻辑和文件读取错误
【发布时间】:2015-09-23 01:05:56
【问题描述】:

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

随着更多信息的到来,我将每 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 排在第一位。 山姆排在最后。

【问题讨论】:

  • 你应该做的是将所有的名字保存到一个数组中然后你安排它们..这样会节省很多时间
  • 我还没开始学习数组。
  • 您在阅读名称时一次显示一个名称,中间没有任何空格。
  • 进行编辑 - 谢谢

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


【解决方案1】:

我建议你使用 array 然后使用算法 sort 函数

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

因此,我们可以将文件中的这些数据保存到该数组中。数组的名称是 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 &lt;algorithm&gt; 然后在数组上使用名为 dataFromFile

的排序方法
sort(begin(dataFromFile),end(dataFromFile));

然后将你拥有的东西重新显示到数组中

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

【讨论】:

  • 这是最好的答案。
  • 如果超过 9 行,此代码将崩溃。为什么不使用向量和 push_back?
  • 因为他在问题中只有9个名字,如果有什么他会增加数组的大小或使用向量
【解决方案2】:

不使用数组,这是最好的解决方案,if 语句出现逻辑错误。

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

接下来,在 while 循环的第四次迭代中,变量的分配变得不合逻辑,并且“Sam”通过 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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-12-09
    • 1970-01-01
    • 1970-01-01
    • 2017-08-01
    • 2020-10-21
    • 1970-01-01
    • 2010-12-10
    • 1970-01-01
    相关资源
    最近更新 更多