【问题标题】:Create a file and read the file创建文件并读取文件
【发布时间】:2016-04-06 08:44:58
【问题描述】:

我需要从文本文件中给出的数字列表中计算数字、平均值、总和、最高和最低的数量。我让它工作,但由于某种原因给了我文件中的数字列表,然后它说最高和最低 = 0 我无法找到帮助。

文件中的数字是

8 50 74 59 31 73 45 79 24 10 41 66 93 43 88 4 28 30 41 13 4 70 10 58 61

代码:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main ()
{
    string STRING;
    ifstream infile;
    infile.open ("Numbers.txt");
    while(!infile.eof()) // To get you all the lines.
    {
        getline(infile,STRING); // Saves the line in STRING.
        cout<<STRING; // Prints our STRING.
    }

    int count = 0;
    float sum = 0;
    float num = 0;
    float solution = 0;

    infile.open("Numbers.txt");

    if (infile)
        infile>>num;

    while (infile && count <25)
    {
        sum=sum+num;
        count++;
        infile>>num;
    }

    if (count <0)
    {
        solution=sum/count;

        cout <<"Number of numbers is:"<<number<<endl;
        cout<<"sum of all number is:"<<sum<<endl;
        cout<<"The average of all numbers in the file is "
        <<solution<<endl;
    }

    int highest;
    int lowest;

    while(infile >> highest)
        highest = count;
     while(infile >> lowest )
         lowest = count;
    cout<<" Highest is:"<<highest<<endl;
    cout <<"Lowest is:"<<lowest<<endl;

    infile.close();


}

【问题讨论】:

  • 你有一个未声明的变量number——我认为它应该是count
  • if (count &lt; 0) 怎么可能是真的? count0 开头,然后您读取的每个数字都会增加它。
  • 你不能再打电话给infile.open(),除非你先打电话给infile.close()。但是你可以使用infile.rewind()
  • 你说你成功了。但是当我尝试你的程序时,它所做的只是打印出文件内容,并说最高和最低都是 0。它从不打印计数、总和或平均值,所以它从不将最后一个数字加两次。您确定您发布的代码是您描述的问题吗?
  • 不要使用 !infile.eof()

标签: c++


【解决方案1】:

删除 如果 (infile) 文件 >> 数量; while (infile && count

【讨论】:

    【解决方案2】:

    第二次从文件中读取列表时,读取的光标仍位于文件末尾。 它需要事先重置到文件的开头。在文件已打开时尝试打开文件不是解决方案。

    你可以简单地使用 seek (但记得清除流的状态!):

    infile.clear();
    infile.seekg(0, ios::beg);
    

    注意:在尝试之后直接读取整个列表之前,您可能不想单独读取第一个数字。

    【讨论】:

      【解决方案3】:

      您的代码中有一些我不理解的内容(例如重复读取文件)和其他完全错误的内容,例如您尝试从文件中提取最高和最低数字的部分或使用number 而不是count。这是一个固定版本(假设是 C++11 兼容的编译器):

      #include <iostream>
      #include <fstream>
      #include <limits>   // for max and min float
      
      using std::cout;
      
      int main ()
      {
          // open file
          std::ifstream infile("Numbers.txt");
          if ( !infile.good() )
          {
              cout << "Error, can't open number's file.\n";
              return -1;
          }
      
          // variables initialization  
          int count = 0;
          float num,
                min = std::numeric_limits<float>::max(),      // to be sure to update it
                max = std::numeric_limits<float>::lowest(),
                sum = 0.0,
                average = 0.0;
          // read all numbers till EOF or invalid input
          while( infile >> num )
          {
              // output the numbers in the same loop
              cout << num << ' ';
              sum += num;
              if ( num < min )
                  min = num;
              if ( num > max )
                  max = num;
              ++count;
          }
      
          if (count > 0)
          {
              cout << "\nThere are " << count << " numbers in the file"
                   << "\nThe sum of all number is: " << sum
                   << "\nThe average of all numbers in the file is: " << sum / count
                   << "\nThe lowest of all numbers is: " << min
                   << "\nThe highest of all numbers is: " << max << '\n';
          }
          else
          {
              cout << "No number read from file";
          }
      
          return 0;
      }
      

      根据你发布的数据,程序的输出是:

      8 50 74 59 31 73 45 79 24 10 41 66 93 43 88 4 28 30 41 13 4 70 10 58 61 
      There are 25 numbers in the file
      The sum of all number is: 1103
      The average of all numbers in the file is: 44.12
      The lowest of all numbers is: 4
      The highest of all numbers is: 93
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-04-13
        • 2015-10-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-12-21
        • 1970-01-01
        相关资源
        最近更新 更多