我怎么让程序减去之前的温度和当前的温度

How would I make program subtract the previous temp and current temp

本文关键字:温度 程序      更新时间:2023-10-16

一个交互式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;
//
    }

}

您在代码中注释了它吗?我不明白

difference = temperature-previousTemp;

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

previousTemp=temperature;