Hello guys... I had an assignment where i needed to find the average temperature.. here is the question

This project asks the students to read input data from a file, do some processing, and direct the output onto the screen.

Use Visual C++ to write your program.

Write a C++ program that reads a series of 12 temperatures (floating-point numbers) from an input file. It should prompt the user for the name of the input file. The input file should be place in the same directory where your source program resides. It should write out on the screen each temperature and 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 to the 4th place after the decimal point. For example, given the following 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

The output on the screen should be:

Please enter the input file name: XXXXX

The 12 temperatures read from file XXXXX is:
34.5
38.6 4.1
42.4 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

The average temperature is: 50.1917


I'm not so good at this and i haven't been paying a lot of attention plus computers are not my strong side... here's how i've started out so far...

#include <iostream>
#include <fstream>
#include <conio.h>
#include <cmath>
using namespace std;
#define in_file "temp.txt" 
int main ()
{

i would appreciate any help... thanks alot.

... here's how i've started out so far...

#include <iostream>
#include <fstream>
#include <conio.h>
#include <cmath>
using namespace std;
#define in_file "temp.txt" 
int main ()
{

I assume you can tell you have done nothing.

Start with opening the file (check for errors)
Use a loop to read the values and display them.

That's the first step.

Yeah that is mostly because i don't know much... sooo what about this..

#include <iostream>
#include <fstream>
#include <conio.h>
#include <cmath>
using namespace std;
#define in_file "temp.txt" 
int main ()
{
ofstream SaveFile("temp.txt");
SaveFile << "34.5 38.6 42.4 46.8 51.3 63.1 60.2 55.9 60.3 56.7 50.3 42.2";
SaveFile.close();

1) get rid of conio.h
2) "Write a C++ program that reads a series of 12 temperatures (floating-point numbers)" -- does your code read?
3) you obviously haven't been taught yet how to format your code -- learn this quickly and well.
4) get away from the computer.

  1. Sit down with pencil and paper and go though your simple assignment and plan out what needs to be done. Step by step.
  2. When you get the step by step done, look at each step and break it down into smaller pieces.
  3. When you can't break it down any farther, write down 12 numbers -- this becomes your 'file' (make the numbers simple.
  4. Now, starting at the top of your steps, follow them.
  5. Do exactly what the step says to do. Do not do what you meant the step to do. If you need to do that, fix the step.
  6. When you can run through all the steps correctly and get the right answer, you are now ready to program.
  7. Convert all the steps into code.
  8. Now, run through the code line by line, just like the steps.
  9. When that works, go back to the computer and start programming.

Yes, this sounds like a pain. But this is programming. It's design and planning. You can't program what you haven't planned out -- you get a jumbled mess that doesn't work.

In your case, you don't know what to do. But the description above tells you exactly what you need to do. You just have to plan...

Ok that was a lot of help... thanks... here's what i've done so far

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
	string line;
 
 
	ifstream openFile("temp.txt");
		if(openFile.is_open())
		{
			while(getline (openFile,line) != NULL);
{
	cout<<line<<endl;
}
		openFile.close();
	}
	else 
		cout <<"Unable to open file";
	return 0;
}

Depends on your point of view.

1) If you are into the coding and testing phase, yes. But don't you have to do math with the data read? Shouldn't they be numbers, not strings?
2) If you followed my advice, you'd still be designing, so you took shortcuts and went right to code.

Don't get me wrong, it can work this way too, but you need to
1) design a small section
2) code that section
3) test that section until it works
4) return to 1 for the next small piece...

The key word is design.

yeah that's where im officially stuck... i've been digging through the book.... this is as far as i can officially go... like i said im really not good with this..

anyone?

commented: NEVER post a bump. We'll get to you when we can. If you want immediate help 24-7 hire a tutor. -4

Are you telling me in all your schooling you've never added a bunch of numbers and calculated an average? I don't believe you. I'm sure you can easily do this on paper. (*hint*)

ok where do i put this (saying that i even did it right)

after declaring sol (as solution)
sol=(34.5 + 38.6 + 42.4 + 46.8 + 51.3 + 63.1 + 60.2 + 55.9 + 60.3 + 56.7 + 50.3 + 42.2)/12

next thing is on the question it stated
"It should write out on the screen each temperature and the difference between the current temperature and the one preceding it."

how would i go on by doing that? Thank you for your help.

Would it look something like this?

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int sol;
int main()
{
	string line;
 
 
	ifstream openFile("temp.txt");
		if(openFile.is_open())
		{

			sol=(34.5 + 38.6 + 42.4 + 46.8 + 51.3 + 63.1 + 60.2 + 55.9 + 60.3 + 56.7 + 50.3 + 42.2)/12; 
			while(getline (openFile,line) != NULL);
{
	cout<<line<<endl;
}
		openFile.close();
	}
	else 
		cout <<"Unable to open file";
		cin.get(),cin.get();
	return 0;
}

Anyone... assignment is due tmw :(

I repeat:

4) get away from the computer
and

  1. Sit down with pencil and paper and go though your simple assignment and plan out what needs to be done. Step by step.
  2. When you get the step by step done, look at each step and break it down into smaller pieces.
  3. When you can't break it down any farther, write down 12 numbers -- this becomes your 'file' (make the numbers simple.
  4. Now, starting at the top of your steps, follow them.
  5. Do exactly what the step says to do. Do not do what you meant the step to do. If you need to do that, fix the step.
  6. When you can run through all the steps correctly and get the right answer, you are now ready to program.
  7. Convert all the steps into code.
  8. Now, run through the code line by line, just like the steps.
  9. When that works, go back to the computer and start programming.

If you want to post (b) when you get it, we can see if you're on the right track. But so far you've followed very little advice.

Yeah it's due tomorrow and im screwed ... Thanks anyway.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.