Advice for reading and writing files

I am wondering if it is possible to read in values from a previous text file that has a string (one word) and an integer(example: Lima 77) on each line, and then write a new file where I can use those values? I have my code working below, but my teacher is a bit specific and after scouring just about every resource known to man I cannot figure it out.

I've tried putting a loop that reads in the values, but the output for the new file is the last city with the Celsius repeated a bunch of times. I also am getting an error that says the fahrenheit isn't initialized. Any advice on what I'm doing wrong or is it not even possible what I'm trying to do?

commented sections are the areas in question.

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
#include <iostream>
#include <fstream> 
#include <string>

using namespace std;

int main() {
	string cityName;
	int fahrenheit;
	int celsius;
	
	ifstream inFS("FahrenheitTemperature.txt"); 
	
	//Add a loop to read in values for cityName and fahrenheit
        //inFS >> cityName >> fahrenheit
        //Close inFS

        ofstream outFS;	
	outFS.open("CelsiusTemperature.txt"); 

	if (!inFS) {  
		cout << "Unable to open file" << endl;
		return 0;
	}
	else { //Have loop for Celsius conversion and outFS 
		while (inFS >> cityName >> fahrenheit) { //Remove inFS
			celsius = round((fahrenheit - 32.0) * (5.0 / 9)); 
			outFS << cityName << " " << celsius << endl; 
		}
	}

	inFS.close(); //Remove close inFS
	outFS.close(); 
	return 0;
}


Essentially teaching myself C++ so any help is appreciated!

EDITED: Rubric with the instructions below.
Develop code to read data from a text file. Read the provided document, FahrenheitTemperature.txt, which includes data on the average yearly temperature for six different cities in degrees Fahrenheit. Note that a space separates each city from its temperature. Assume the city is one word. Also assume the provided temperature is presented as an integer. Consider the following steps as you work:
Open the provided file so it is ready to be read. Remember the file is named FahrenheitTemperature.txt. Read data from the provided file. Remember, to read this file you will need to declare a variable. Begin by reading the first value and putting it in the first variable. Then read the next value and put it in the second variable.
Once this is complete, be sure to close the file. This releases the file so it can be used again.

Develop code to write data to a text file. Title the new document you are creating CelsiusTemperature.txt. Consider the following steps as you work:
Declare a variable to point to the file that will be written to. Create the code instructions for writing data to the new output file. In this new file, include space for both the name of the city and the temperature in Celsius for each city included in the original input file. You will need to complete the Fahrenheit-to-Celsius conversion calculation before you write to the new file. Use the following formula to make this conversion. Note that °F represents the temperature in degrees Fahrenheit while °C represents the temperature in degrees Celsius. Close the file once you are done writing to it. If you attempt to look at the results in the file before completing this step, your file may appear empty.
Last edited on
but my teacher is a bit specific

In what way?

Add a loop to read in values for cityName and fahrenheit

If you want to read the input values separately from the output of the "result" you will need some kind of data structure to hold the input values for post read processing.

1
2
3
4
5
6
	ifstream inFS("FahrenheitTemperature.txt"); 
	
	...

        ofstream outFS;	
	outFS.open("CelsiusTemperature.txt"); 


Be consistent, but prefer using the constructor:

1
2
3
4
5
	ifstream inFS("FahrenheitTemperature.txt"); 
	
	...

        ofstream outFS("CelsiusTemperature.txt"); 


Also you should be checking that the input file before you try to open the output file, and don't forget to check that the output file opened correctly.

There is also no need to manually close the files, the destructors of the streams will do this automatically when the streams go out of scope.


I also am getting an error that says the fahrenheit isn't initialized.


You should also be getting a message about your use of the round function. Where is the round() function defined/implemented?

Based off our instructions given, it seemed to me that he wanted us to open and read the file, read in the values, close it. then write new file, do conversion, and output values to new file, close file --- all on same cpp with one function. I am a very literal person so I could just be reading into the instructions too much.

Thank you for the information though, I will try to play around with it a bit more and use your advice. My teacher is very vague and my textbook is crap, so this has been a process trying to teach myself.

I am getting a warning for the round function too, yes. Everything was still working so I wasn't too worried, but will make sure to change that as well.

Thank you again! It is always helpful to have someone explain things not in textbook terms.
Based off our instructions given

Perhaps you should consider posting the instructions?

Consider using a std::vector for the container that will hold the items from the input file.

I am getting a warning for the round function too, yes. Everything was still working so I wasn't too worried, but will make sure to change that as well.

You need to fix all warnings and errors before you continue. Always start by fixing the first error/warning, recompile, rinse, repeat. Always treat warnings as errors (they usually are errors in disguise)!

I really don't see any need for rounding the result, I'd start without any rounding to insure the results are actually reasonable.

If you need help finding the warning and errors post the complete error/warning messages, all of them, exactly as they appear in your development environment. The error messages will have important information embedded within them to aid in finding and fixing the problems.

I have edited the original post with the instructions given to me.

Totally makes sense, I definitely won't let them slide anymore. I don't know why I was rounding. I think I just figured I should, but probably don't need to so I will just avoid that.
Some pedantic-ness:

I always write floating point literals with digits before and after the decimal point, as in: 5.0 , not 5 . This reinforces the idea that the value is double. And it can help prevent inadvertent integer division.

5.0/9.0 is a constant, express it as such in the code:

constexpr double TempConvFactor {5.0/9.0};

Doing so before the loop will prevent this expression being evaluated every time. The optimiser may fix this, but try to prevent the tools from doing extra work, even if it is only a little bit.


Try not to have using namespace std; There is plenty written about that already, I am not going to repeat it here. Just type std:: before every std thing. That is what professionals do, may as well get onboard early :+) Can setup keyboard shortcuts in your editor.

So putting std:: in your code would have helped with the call to round function - did you mean std::round? If not, it would prompt you to write a definition for that function.
Topic archived. No new replies allowed.