Homework Help Using Files

This is a discussion on Homework Help Using Files within the C++ Programming forums, part of the General Programming Boards category; Hi, This is only my second program, so please be patient with me My assignment is to write a program ...

  1. #1
    Registered User Trekkie's Avatar
    Join Date
    Oct 2005
    Posts
    20

    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!

  2. #2
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    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!
    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!
    Last edited by Enahs; 10-12-2005 at 07:38 PM.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    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.

  4. #4
    Registered User Trekkie's Avatar
    Join Date
    Oct 2005
    Posts
    20

    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

  5. #5
    Cat Lover
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    109
    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.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Create Copies of Files
    By Kanshu in forum C++ Programming
    Replies: 13
    Last Post: 05-09-2009, 07:53 AM
  2. Reading .dat files from a folder in current directory...
    By porsche911nfs in forum C++ Programming
    Replies: 7
    Last Post: 04-04-2009, 09:52 PM
  3. *.cpp and *.h files understanding
    By ElastoManiac in forum C++ Programming
    Replies: 4
    Last Post: 06-11-2006, 04:45 AM
  4. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 10:46 AM
  5. Multiple Cpp Files
    By w4ck0z in forum C++ Programming
    Replies: 5
    Last Post: 11-14-2005, 01:41 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21