【发布时间】:2015-10-07 23:22:09
【问题描述】:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
int a , b , c , i , n;
int d = 0;
ifstream myfile;
myfile.open("Duomenys1.txt");
myfile >> n;
for (int i = 0; i < n; i++ )
{
myfile >> a >> b >> c;
d += (a + b + c)/3 ;
}
ofstream myotherfile;
myotherfile.open ("Rezultatai1.txt");
myotherfile << d;
myotherfile.close();
myotherfile.close();
return 0;
}
程序应读取 3 (3 为 n) 行数字 (5 7 4 ; 9 9 8; 8 7 8),行分别汇总并在 Rezultatai1 中给出 3 个不同的平均值 (7 ; 9 ; 8) .txt 文件。但我只得到 -2143899376 结果。
问题不在于巨大的数字,我需要程序在输出文件中分别给出每一行的平均数,以便在输出文件中写入 (7 ; 9 ; 8)
【问题讨论】:
-
您显示的程序是您实际运行的程序吗?您为输入文件显示的数据是实际数据吗?然后你应该在输出文件中得到
20。 -
对我来说很好用(结果是
5+8+7=20)。你的输入文件是什么样子的?顺便说一句,整数运算给出(9+9+8)/3=8而不是9。 -
@Walter 我只是使用 OP 提供的数字作为平均值,尽管它们是错误的。
-
@JoachimPileborg 从不相信 OP
-
@ParanoidParrot 你的程序没有四舍五入,你只使用整数运算,所以结果被截断。