Solved

Problem writing C++ Program that use data files for input and output.

Posted on 2006-11-13
6
627 Views
Last Modified: 2008-01-09
Hello,

I seem to be stuck writing this program that inputs a series of 12 temperatures from the user. It should write out on the file "tempdata.dat" each temperature and the difference between the current temperature and the one preceeding it. The differne is not  output for the first temperature this is input. At the end of the program, the average temperature should be displayed for the user via cout. Here's 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  

File tempdata.dat would contain:

34.5

38.6   4.1

42.2   3.8

46.8   4.4

51.3   4.5

63.1   11.8

60.2   -2.9

55.9   -4.3

60.3   4.4

56.7   -3.6

50.3  -6.4

42.2  -8.1

This is what I have come up with so far but I got stuck as to what to do next about the computation in getting the difference between the temperatures. Can someone please help.

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
    // Declare and open files
    ifstream inData;
    ofstream outData;
   
    inData.open("tempdata.dat")
    outData.open("tempdata.out")
   
    //Read in data from file inData
    inData >> 34.5 >> 38.6 >> 42.4 >> 46.8 >> 51.3 >> 63.1 >> 60.2 >>
           >> 55.9 >> 60.3 >> 56.7 >> 50.3 >> 42.2;
   
    //Output information in required formats
0
Comment
Question by:DeaconRev2
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 17935399
Do you understand how the >> operator works?
0
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 500 total points
ID: 17937426
It seems to me that you yoou didn't read the requirements correctly ....

>>>> that inputs a series of 12 temperatures from the user.

I would say you would need a loop to get 12 temperatures from the user, something like

   for (int i=0; i < 12; ++i)
   {
        // prompt for input
     
        // read input from cin to  temperature array at position i
   }

>>>> It should write out on the file "tempdata.dat" each temperature and the difference
>>>> between the current temperature and the one preceeding it.

That means that you need a further loop where you output the temperature array at position j and the difference between temperature array at position j and  temperature array at position j-1. Note, for the latter you have to check that i is greater 0.

   // open tempdata.dat for output

   for (int j=0; j < 12; ++j)
   {
        // output to file temperature array at position j  + space separator  
        // if j > 0 output to file temperature array at position j - temperature array at position j - 1
   }

>>>> At the end of the program, the average temperature should be displayed for the user via cout.

Make another loop and add all temperature values from array. Then output the stotal divided by 12.

Regrads, Alex
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
C++ standard library based binary archive format 6 112
C++ Class Serialization, Encapsulation and Formatted Output 8 110
VS2015 Redefinition errors 4 105
learn programming 8 95
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question