【问题标题】:Sum of data from a text file文本文件中的数据总和
【发布时间】:2016-02-09 14:40:04
【问题描述】:

我正在尝试从一个看起来像这样的文本文件中添加数字:

player1
132
41

player2
1150
323

player3
60
2

输出应该是这样的:

41
41

323
364

2
366

其中第二个数字将显示前一个数字的第一个数字的总和。但是,我最终得到了这个:

41
0

323
0

2
0

数据似乎显示正确,但我不明白为什么总和不在同一个循环中。这是我的代码:

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

struct player{
string name;
int nbmatches;
int nbvictories;
};

player data;
int main(){

ifstream readFile;
readFile.open("note.txt");
if (readFile.fail()){
    cout << "not found" << endl;
}
else
{
    readFile.clear();
    while (!ws(readFile).eof()){
        readFile >> data.name
            >> data.nbmatches
            >> data.nbvictories;

        int totalvictories = 0;
        data.nbvictories += totalvictories;
        cout << data.nbvictories << endl;
        cout << totalvictories << endl;
        cout << endl;
    }
}
system("pause");
}

【问题讨论】:

  • 您正试图同时做两件事:从文件中读取数据,并保持运行总和。分别处理。
  • data.nbvictories += totalvictories; 这是倒退吗?
  • "int totalVictory = 0" 需要在循环之外
  • 您将零添加到data.nbvictories。这是因为您在每次迭代中都在初始化 totalvictories

标签: c++ file text while-loop sum


【解决方案1】:

您在以下代码中的问题是

int totalvictories = 0; //<- a non-static variable with local scope 
data.nbvictories += totalvictories; //<- you are adding zero every iteration

这个data.nbvictories 每次迭代都会修改两次。一次是用从文件中获得的数字分配它,第二次是用+= 分配它,因为totalvictories = 0 没有影响。如果totalvictories应该存储总数,那么这个变量应该修改而不是data.nbvictories,因此,将语句切换为totalvictories += data.nbvictories;

这是代码

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

struct player{
string name;
int nbmatches;
int nbvictories;
};

player data;
int main(){

    ifstream readFile;
    readFile.open("note.txt");
    if (readFile.fail()){
        cout << "not found" << endl;
    }
    else
    {
        readFile.clear();
        while ( readFile >> data.name >> data.nbmatches >> data.nbvictories   ){

            //int totalvictories = 0;
            static int totalvictories = 0;
            //data.nbvictories += totalvictories;
            totalvictories += data.nbvictories; 

            cout << data.nbvictories << endl;
            cout << totalvictories   << endl;
            cout << endl;
        }
    }
    system("pause");

    return 0;
}

结果是

41
41

323
364

2
366

额外:避免使用system("pause");eof()

【讨论】:

    【解决方案2】:

    在 while 循环之前移动 int totalvictories = 0,每次迭代都将其重置为 0。

    【讨论】:

      【解决方案3】:

      "totalvictories" 必须在 while 之外声明

      int totalvictories = 0;
      while (!ws(readFile).eof()){
          readFile >> data.name
              >> data.nbmatches
              >> data.nbvictories;
      
          data.nbvictories += totalvictories;
          cout << data.nbvictories << endl;
          cout << totalvictories << endl;
          cout << endl;
      }
      

      【讨论】:

      • 不仅totalvictories需要在循环外初始化,中间的那行要颠倒:totalvictories += data.nbvictories
      • 您可以将其声明为静态变量。此外,您可以将两个答案合并为一个答案。
      • 你是对的。我不擅长编辑文本。谢谢你的关心。
      猜你喜欢
      • 2015-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多