【问题标题】:Calculation with values from a text file and write into another text file使用文本文件中的值进行计算并写入另一个文本文件
【发布时间】:2016-05-22 03:30:00
【问题描述】:

我想从一个文本文件中获取一些值,并将这些值与 C++ 中的一个固定数字相乘。然后我想将每个值的解决方案写入另一个文本文件。这是我的代码:

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

std::ifstream infile("Test_60.txt");
std::ofstream outfile("Output.txt");

int main(int argc, char** argv)
{
    double max, min, d, s, m;
    max = 0;
    min = 1;
    double x, y, z, R, G, B;
    while (infile >> x >> y >> z >> R >> G >> B)
    {
        if (max < x)
        {
            max = x;
        }

        if (min > x)
        {
            min = x;
        }

        d = max - min;
        s = 60 / d;
        m = s*x;
    }

    outfile << m << endl;
    infile.close();
    outfile.close();

    system("pause");
    return 0;
}

最小值和最大值已经起作用,但是当我尝试将其写入文本文件时,我只得到一个 x(或 m)的值。 最后,我想要一个文本文件,其中包含 x 和 y 的乘积值以及 z、R、G、B 输入文件中的值。

最好的问候。

【问题讨论】:

  • 您的输出流调用需要在 while 循环内,不是吗?
  • 我认为您需要两个循环,一个用于查找最大值和最小值,然后在计算乘法因子后,一个用于评估 x(和 y?)的新值并将它们写入第二个文件。

标签: c++ text-files ifstream


【解决方案1】:

你必须发表声明

max = 0;
min = 1;

进入 while 循环,以便确保最小/最大重置。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-20
    • 2012-05-24
    • 1970-01-01
    相关资源
    最近更新 更多