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.