# Homework Help Using Files

• 10-12-2005
Trekkie
Homework Help Using Files
Hi,

This is only my second program, so please be patient with me :)

My assignment is to write a program that inputs 12 temperatures from the user. It should write out on file "tempdata.dat" each temp and the difference between the current temp and the one preceding it. The difference is no output for the first temp that is input. At the end of the program, the average temp should be displayed for the user via cout. The given input data:

34.5
38.6
42.4
46.8
51.3
63.1
60.2
55.9
60.3
56.7
50.3
42.2

I am not sure where to start with this program. How do I get my program to calculate the difference between the temperatures?? This is what I have so far:

Code:

``` #include <iostream> #include <iomanip> #include <fstream> #include <string> using namespace std; int main() {                 ifstream indata;         ofstream outdata;         indata.open("tempin.dat"); //temperature data         outdata.open ("tempdata.dat"); //output data         string temperature;//temperature value                 ifstream tempin;//holds temperataure data         ofstream tempdata;//holds output temperature data                 outdata <<         tempin.close();         tempdata.close();         return 0; }```
How do I format the "outdata"?

Any help would be appreciated!
• 10-12-2005
Enahs
You say your assignment is to take the data from the user. So I see no need for the instream, just get the data from cin. I will stop with that, because that simplifies it quite a bit. If you still can not figure it out just say so and more help can come your way!
Quote:

I am not sure where to start with this program. How do I get my program to calculate the difference between the temperatures??
The diff is just the current minus the previous. Now from my math back ground I would say it is the absloute value of the current - the previous; but really that should not matter much.

*edit*
Quick tip; you are working with numbers. There is no reason to use strings! Think numbers!
• 10-12-2005
Daved
I would start with two double variables, one for the current temp and one for the previous one. A string variable doesn't help if you need to do math on the input.

I would then write the program but using cin and cout to make sure the logic is correct. Once that was working, I'd switch to use file streams like your shell code has above. To format the outdata, just use << to output the value and the difference like you would with cout.
• 10-12-2005
Trekkie
Another Question
Thanks for giving me some ideas.

I have another question. Can I have use outdata.open ("tempdata.dat"); without using an indata.open statement?

For the cin portion of the program do I need to put:

istream cin;
ostream cout;

and then:

cin >>

Thanks
• 10-12-2005
Dweia
1. Yes, that is perfectly legal.

2. cin and cout are declared in the std namespace, so you don't have to declare them like fstream, you can just use them.
• 10-12-2005
Daved
BTW, my advice to use cin and cout first assumed that you were familiar with them. If you haven't learned how to use them either, then it is fine to start with the fstreams.