Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++: Is it possible to read in values from a text file then close it that file, and in the same function write a new

C++: Is it possible to read in values from a text file then close it that file, and in the same function write a new file where I can use those values. I am trying to avoid a vector or anything like that. My code is below and is working, but I am trying to see if I can get it a certain way because my teacher is particular. I commented and bolded the areas that I would like to change if possible.

I should add that the text file I'm reading in has city names (one word) listed with a temperature, for example "Lima 66"

#include #include //Enables use of the ifstream class #include

using namespace std;

int main() { ifstream inFS; //Input file stream-reads file string cityName; int fahrenheit; int celsius;

inFS.open("FahrenheitTemperature.txt"); //Opens file

//Read in cityName and fahrenheit with inFS >>

//Close inFS ofstream outFS; outFS.open("CelsiusTemperature.txt"); //Writes new file

while (inFS >> cityName >> fahrenheit){ //Remove inFS >> here if possible celsius = round((fahrenheit - 32.0) * (5.0 / 9)); outFS << cityName << " " << celsius << endl; }

inFS.close(); //remove if can outFS.close();

return 0; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

C++ Database Development

Authors: Al Stevens

1st Edition

1558283579, 978-1558283572

More Books

Students also viewed these Databases questions

Question

Why are absolute amounts of limited use when comparing companies?

Answered: 1 week ago