CelsiusTemperature.txt.

cpp

School

Southern New Hampshire University *

*We aren’t endorsed by this school

Course

210

Subject

Computer Science

Date

Nov 24, 2024

Type

cpp

Pages

2

Uploaded by DrRoseLoris32

Report
#include <iostream> #include <fstream> #include <vector> using namespace std; int FahrenheitToCelsius(int f){ //function to convert from Fahrenheit to Celsius int tempF = f; int tempC = (tempF - 32) * 5 / 9; return tempC; } int main() { ifstream inFS; // Input file stream ofstream outFS; // Output file stream string city1, city2, city3, city4, city5; //declaration of variables for each city int cityTempFah1, cityTempFah2, cityTempFah3, cityTempFah4, cityTempFah5; //declaration of variables for Fahrenheit temperature int cityTempCel1, cityTempCel2, cityTempCel3, cityTempCel4, cityTempCel5; //declaration of variables fo Celsius temperature inFS.open("FahrenheitTemperature.txt"); //function to open file to read if (!inFS.is_open()) { cout << "Could not open file" << endl; return 1; // 1 indicates error } cout << "Reading..." << endl; inFS >> city1 >> cityTempFah1; inFS >> city2 >> cityTempFah2; inFS >> city3 >> cityTempFah3; inFS >> city4 >> cityTempFah4; inFS >> city5 >> cityTempFah5; inFS.close(); //closes file cout << city1 << " " << cityTempFah1 << endl; //output of data read from file cout << city2 << " " << cityTempFah2 << endl; cout << city3 << " " << cityTempFah3 << endl; cout << city4 << " " << cityTempFah4 << endl; cout << city5 << " " << cityTempFah5 << endl; cout << "\n" << "Converting from Fahrenheit to Celsius" << "\n" << endl; //call to function to make sure it works //cout << city1 << " " << FahrenheitToCelsius(cityTempFah1); cityTempCel1 = FahrenheitToCelsius(cityTempFah1); //assignment of conversion to a new variable cityTempCel2 = FahrenheitToCelsius(cityTempFah2); cityTempCel3 = FahrenheitToCelsius(cityTempFah3); cityTempCel4 = FahrenheitToCelsius(cityTempFah4); cityTempCel5 = FahrenheitToCelsius(cityTempFah5); outFS.open("CelsiusTemperature.txt"); //function to open output stream to a new file if (!outFS.is_open()) { cout << "Could not open file \"CelsiusTemperature.txt\" " << endl; return 1;
} outFS << city1 << " " << cityTempCel1 << endl; //writing conversions to new file outFS << city2 << " " << cityTempCel2 << endl; outFS << city3 << " " << cityTempCel3 << endl; outFS << city4 << " " << cityTempCel4 << endl; outFS << city5 << " " << cityTempCel5 << endl; outFS.close(); //closes file inFS.open("CelsiusTemperature.txt"); //function to open to read data of new file if (!inFS.is_open()) { cout << "Could not open file" << endl; return 1; // 1 indicates error } cout << city1 << " " << cityTempCel1 << endl; //output of data read from file cout << city2 << " " << cityTempCel2 << endl; cout << city3 << " " << cityTempCel3 << endl; cout << city4 << " " << cityTempCel4 << endl; cout << city5 << " " << cityTempCel5 << endl; inFS.close(); //closes file return 0; }
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help