Having some difficulty understanding file io

An interactive C++ program whose input is a series of 12 temperatures from the user. It should write out on file tempdata.dat each temperature as well as the difference between the current temperature and the one preceding it. The difference is not output for the first temperature that is input. At the end of the program, the average temperature should be displayed for the user via cout.

Here is some code I wrote, Am I'm on the right track?
#include <iostream>
#include <fstream>

using namespace std;

int main() {

ofstream file; //declares a file of type ofstream
file.open("temperaturefile.txt"); //opens the file with open method
int temperature = 0;
int difference = 0;

file << temperature;

while(file)
{
cout << temperature << endl;
// difference = difference - temperature;
}



}
You're gonna need some sort of input. If your professor wants you to read in from a file, you'll need an ifstream object. Syntax is similar to how ofstream objects work except the operators go the other way >>. Also I would suggest you call ofstream object.close() at the end of your program.

Also you don't need to initialize your variables when dealing with I/O streams in your case. It's like declaring a variable and calling cin. You're immediately giving that variable a value, so why initialize to a value you don't want? It'll read and give the temperature variable values from your file.
Here is some of the new code I updated.

#include <iostream>
#include <fstream>

using namespace std;

int main() {

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

file << temperature;

while (counter < 12)
{

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



}
Last edited on
You should use file.open() from an ifstream object, but your wording is a bit vague in your program. Do you want to directly input from the console 12 different temperatures? Or are you reading them from a file?

http://www.cplusplus.com/reference/fstream/ifstream/?kw=ifstream
http://www.cplusplus.com/reference/fstream/ofstream/?kw=ofstream
In a majority of cases you should not need to use .open() or .close() - just let RAII work as intended:
1
2
3
4
5
6
7
8
if(std::ifstream in {"file.txt"})
{
    //file is usable
}
else
{
    std::cerr << "Unable to open file.txt" << std::endl;
}
This is more updated code this actually runs and subtracts the program.
#include <iostream>
#include <fstream>

using namespace std;

int main() {

int average = 0;
int counter = 0;
int previousTemp = 0;
ofstream file; //declares a file of type ofstream
file.open("tempdata.dat"); //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;
previousTemp = temperature;
file << temperature;
cout << difference << endl;
// average = previousTemp + temperature/12;

}

average = previousTemp + temperature/12;
cout << "The average is" << average << endl;






}
Topic archived. No new replies allowed.