如何使程序减去先前的临时温度和当前温度

时间:2015-10-07 17:24:16

标签: c++ algorithm

交互式C ++程序,其输入是来自用户的一系列12个温度。它应该在文件tempdata.dat上写出每个温度以及当前温度和它之前的温度之间的差异。输入的第一个温度不输出差值。在程序结束时,应通过cout为用户显示平均温度。

这是我到目前为止所拥有的:

#include <iostream>
#include <fstream>

using namespace std;

int main() {

    int counter = 0;
    int previousTemp;
    ofstream file; //declares a file of type ofstream
    file.open("temperaturefile.txt"); //opens the file with open method
    int temperature = 0;
    int difference = 0;
    cout << "Enter 12 temperatutes" << endl;

    file << temperature;

    while (counter < 12)
    {

        cin >> temperature;
        counter++;
//        difference = temperature-previousTemp;
//        cout << difference << endl;
//
    }



}

1 个答案:

答案 0 :(得分:1)

您的代码中是否有评论?我不明白

difference = temperature-previousTemp;

您可以在循环结束时跟踪previousTemp。 在循环中的所有内容之后

previousTemp=temperature;