【问题标题】:How would I make program subtract the previous temp and current temp我将如何让程序减去以前的温度和当前温度
【发布时间】:2015-10-08 01:30:36
【问题描述】:

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



}

【问题讨论】:

  • 然后呢?您还没有提出实际问题。
  • 我更新了程序。抱歉,我还是堆栈溢出的新手

标签: c++ algorithm


【解决方案1】:

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

difference = temperature-previousTemp;

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

previousTemp=temperature;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-21
    • 2016-07-16
    • 1970-01-01
    • 1970-01-01
    • 2019-03-25
    • 2015-04-29
    • 2013-02-05
    • 2017-05-05
    相关资源
    最近更新 更多