Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ - Did I write this code correctly? I am unable to output theconversion I created, so I am not sure if I am missing

C++ - Did I write this code correctly? I am unable to output theconversion I created, so I am not sure if I am missing something.Here are the instructions:

Prompt

In this assignment, you are presented with a text document thatincludes six cities and their average yearly temperature inFahrenheit. Your goal is to read that data, convert it to Celsiususing the provided formula, and then write that new data to its ownfile.

To begin your work, open Visual Studio and create a new C++project. Save the provided FahrenheitTemperature.txt document in alocation where you will easily be able to access it while you workin Visual Studio. For this assignment, you will be submitting onlyyour C++ (.cpp) file. You do not need to submit the final converteddata; the C++ code you create just needs to be able to generatethat file.

Specifically, you must address the following rubriccriteria:

  • Develop code to read data from a text file.Your work should be completed using C++. Read the provideddocument, FahrenheitTemperature.txt, which includes data onthe average yearly temperature for six different cities in degreesFahrenheit. Note that a space separates each city from itstemperature. Assume the city’s name does not include any spaces orspecial characters (the name should consist of only a single 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 thefile is named FahrenheitTemperature.txt. Watch out for the classyou use, and make sure it is for reading a file and not writing toa file.
    • Read data from the provided file. Remember, to read this fileyou will need to declare a variable. Begin by reading the firstvalue and putting it in the first variable. Then read the nextvalue and put it in the second variable.
    • Once this is complete, be sure to close the file. This releasesthe file so it can be used again.
  • Develop code to write data to a text file.Your work should be completed using C++. Title the new document youare creating CelsiusTemperature.txt. The name of theoutput file needs to be different from the name of the input fileso you do not overwrite and erase the input file. Consider thefollowing steps as you work:
    • Declare a variable to point to the file that will be writtento. Watch out for the class you use, and make sure it is forwriting to a file and not reading a file.
    • Create the code instructions for writing data to the new outputfile. In this new file, include space for both the name of the cityand the temperature in Celsius for each city included in theoriginal input file. You will need to complete theFahrenheit-to-Celsius conversion calculation before you write tothe 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 attemptto look at the results in the file before completing this step,your file may appear empty.

***************************************

Here is the .txt file:

Toronto 47Lima 66Istanbul 57Lagos 81Shanghai 61Sydney 64

***************************************

Here is my code:

#include
#include //enables use of ifstream class
#include

using namespace std;

int main() {
//Declaring object/variables
ifstream inFS; //input filestream
ofstream outFS; //output filestream
string cityName; //Name of cityfrom file
int tempFarenheit; //Farenheit temp
double tempCelsius; //Celsius temp(double because when calculating, there will be a decimal)

//Opening Farenheit file
inFS.open("FarenheitTemperature.txt");

//Creating Celsius file
outFS.open("CelsiusTemperature.txt");

//Creating a while loop that readsFarenheitTemperature.txt info, converts temp from F toC
//using formula provided, then writes new infoto CelsiusTemperature.txt file
while (inFS >> cityName >>tempFarenheit) {
tempCelsius = (tempFarenheit- 32) * (5 / 9);
outFS << cityName<< " " << tempCelsius << endl;
}

//Closing files
inFS.close();
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

Step: 3

blur-text-image

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

Design Operation And Evaluation Of Mobile Communications

Authors: Gavriel Salvendy ,June Wei

1st Edition

3030770249, 978-3030770242

More Books

Students also viewed these Programming questions

Question

9-3. What are the different types of tariffs?

Answered: 1 week ago