File IO, Fahrenheit conversion

"Write a program which reads stream of Fahrenheit from a file and writes the corresponding Celsius to another file. User should be prompted for input and output file names. Function named convert_to_celsius( ) must be called to read all Fahrenheit and convert to Celsius. You must pass file input and output stream as arguments to convert_to_celsius() function. Check that the files are opened successfully before using them".

The file doesn't open at all. I'm not sure why this program doesn't run.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
 #include <iostream>
#include <fstream>
using namespace std;

void convert_to_celsius(ofstream& ofstream);

int main()
{
	ifstream input_file;
	ofstream output_file;
	
	input_file.open("fahrenheit.txt"); 

	if (input_file.fail())
	{
		cout << "Input file fahrenheit.txt failed to open!\n";
		return 1;
	}

	output_file.open("celsius.txt");
	if (output_file.fail())
	{
		cout << "Output file celsius.txt failed to open!\n";
		return 1;
	}

	convert_to_celsius(output_file);
	input_file.close();
	output_file.close();
	return 0;
}


	void convert_to_celsius(ofstream& ofstream)
	{
		
		double fahrenheit;
		double celsius;

		cout << "Enter the degrees in Fahrenheit: " << endl;
		cin >> fahrenheit;

		celsius = (fahrenheit - 32) * 5.0 / 9.0;

		ofstream << celsius << endl;
	}
change line 9 to ifstream input_file("fahrenheit.txt");
change line 10 to ofstream output_file("celsius.txt");
and remove statements on line 12 and 20.
Last edited on
Topic archived. No new replies allowed.