Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何在c++;text.stream_C++ - Fatal编程技术网

C++ 如何在c++;text.stream

C++ 如何在c++;text.stream,c++,C++,如何对案例5中的数字平均值进行编码?我已经将字符串转换为int。如何从名为“dane.txt”的文本文件中计算数字的平均值 添加两个变量以跟踪数字的计数,以及将所有数字相加得到的总数(为英文名称道歉) 然后在循环结束时,您可以将总数除以计数,这就是您的平均值 int total = 0; int count = 0; ... while(...)

如何对案例5中的数字平均值进行编码?我已经将字符串转换为int。如何从名为“dane.txt”的文本文件中计算数字的平均值


添加两个变量以跟踪数字的计数,以及将所有数字相加得到的总数(为英文名称道歉)

然后在循环结束时,您可以将总数除以计数,这就是您的平均值

                int total = 0;
                int count = 0;
                ...
                while(...)
                {
                        ...
                        string str = a;
                        total += atoi(str.c_str());
                        ++count;
                }
                cout << "average is " << (double)total/count << '\n';
int-total=0;
整数计数=0;
...
而(…)
{
...
字符串str=a;
总数+=atoi(str.c_str());
++计数;
}

你能写一个函数来计算一个整数数组的平均值吗?我能,但我不知道如何用textfile中的数字来做数组好,听起来不错。所以你的问题是在C++中创建一个动态整数数组。你知道std::vector是什么吗?@RoQuOTriX你不知道;我不需要一个数组来计算平均数,你只需要把这些数字相加,然后计算出有多少个。我不知道。我在学校没有过,是的,如果要求对平均值进行积分,则不需要进行
(双)
转换,并应用四舍五入。
                int total = 0;
                int count = 0;
                ...
                while(...)
                {
                        ...
                        string str = a;
                        total += atoi(str.c_str());
                        ++count;
                }
                cout << "average is " << (double)total/count << '\n';