Question

In this Final program you are going to use several concepts you learned in CSC 101...

In this Final program you are going to use several concepts you learned in CSC 101 class, especially operations on or using FILES , LOOPS and FUNCTIONS. You need to implement C++ code that will read data from a file and process it as outlined below and write the processed data to an output file. You can refer to the file input.txt for how the data is made available to you and output.txt for how to write out the processed data. Your code will be tested using an input file with different values. The example input file shows one temperature value along with one word (Celsius or Fahrenheit) per line denoting its scale. The last line has a temperature value and the word Last. You need to ignore the temperature on this line since it doesn’t say whether it is Celsius or Fahrenheit). First determine how many lines are in the file. Your loop(s) will run this many times. Then read each line of the input file (in a loop) and if the word you read is Celsius then convert the temperature to Fahrenheit. If the word is Fahrenheit then convert the temperature to Celsius. Write two functions called convertFtoC and convertCtoF for performing the conversions. Pass input(s) by value and return the converted value using a return statement. As you read each line from the input file, call the appropriate function to convert the value and write the old value, its scale, new value and its scale to the output file. Close the input and output files after the loop. Maximum points 100. For 30 bonus points use arrays. Hint: You may have to open and close the input file more than once. input: 50 Celsius 5 Celsius 8 Celsius 50 Fahrenheit 80 Fahrenheit 10 Last sample output: 50 Celsius 122 Fahrenheit 5 Celsius 41 Fahrenheit 8 Celsius 46.4 Fahrenheit 50 Fahrenheit 10 Celsius 80 Fahrenheit 26.67 Celsius

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Thanks for the question.


Here is the completed code for this problem. Comments are included so that you understand whats going on. let me know if you have any doubts or if you need anything to change.


Thank You !!

=========================================================================================

#include<iostream>
#include<fstream>
#include<string>


using namespace std;

double convertFtoC (double);
double convertCtoF (double);
// reads an input file and returns the number of lines
int getlines(const char* filename );
int getlines(const char* filename) {

ifstream myfile (filename);
int line_count=0;
double temp;
string scale;
while(1) {

myfile>>temp>>scale;
if(scale.compare("Last")==0 || scale.compare("last")==0) {
myfile.close();
return line_count;
}
line_count+=1;
}
}
// convert from F to C
double convertFtoC (double T) {
return (T-32)*5/9;
}
// convert from C to F
double convertCtoF (double T) {
return 32+ 9*T/5;
}


int main() {
  
// update the below line to point to the input file and output file
char * inputFile = "F:\\input.txt";
char * outputFile="F:\\output.txt";
  
int line_count = getlines(inputFile);
  
ifstream input;
input.open(inputFile);
ofstream output;
output.open(outputFile);

string scale;
double temperature;
for(int line=1; line<=line_count; line++) {

input>>temperature>>scale;
if(scale.compare("Celsius")==0) {
double toF = convertCtoF(temperature);
output<<temperature<<" Celsius "<<toF<<" Fahrenheit"<<endl;
} else {
double toC = convertFtoC(temperature);
output<<temperature<<" Fahrenheit "<<toC<<" Celsius"<<endl;
}

}
cout<<"Successfully converted."<<endl;
// close both the input and output file
input.close();
output.close();

}

===============================================================================

thanks !

Add a comment
Know the answer?
Add Answer to:
In this Final program you are going to use several concepts you learned in CSC 101...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Use C++ and must be abke to compile on Visual Studios 9:27 ToDo Final Exam Grade...

    Use C++ and must be abke to compile on Visual Studios 9:27 ToDo Final Exam Grade Detail You need to implement C++ code that will read data from a file and process it as outlined below and write the processed data to an output file. You can refer to the file input.txt for how the data is made available to you and output.txt for how to write out the processed data. Your code will be tested using an input file...

  • B - Temperatures You may use the CodeCheck IDE directly to write this program. (Your program...

    B - Temperatures You may use the CodeCheck IDE directly to write this program. (Your program will be graded by CodeCheck). Here are the requirements: Complete the class Temperatures (you must use this exact name to pass CodeCheck) so that it prompts the user for a temperature value, followed by a character that represents the type of temperature. The second input value is the string "F" for Fahrenheit or "C" for Celsius. If the string is an "F", the temperature...

  • Write a program that would ask the user to enter an input file name, and an...

    Write a program that would ask the user to enter an input file name, and an output file name. Then the program reads the content of the input file, and read the data in each line as a number (double). These numbers represent the temperatures degrees in Fahrenheit for each day. The program should convert the temperature degrees to Celsius and then writes the numbers to the output file, with the number of day added to the beginning of the...

  • FOR PYTHON Write a Python program that can convert a Fahrenheit temperature to Celsius, or vice...

    FOR PYTHON Write a Python program that can convert a Fahrenheit temperature to Celsius, or vice versa. The program should use two custom functions, f_to_c and c_to_f, to perform the conversions. Both of these functions should be defined in a custom module named temps. Custom functionc_to_f should be a void function defined to take a Celsius temperature as a parameter. It should calculate and print the equivalent Fahrenheit temperature accurate to three decimal places. Custom function f_to_c should be a...

  • Write a Python program that can convert a Fahrenheit temperature to Celsius, or vice versa. The...

    Write a Python program that can convert a Fahrenheit temperature to Celsius, or vice versa. The program should use two custom functions, f_to_c and c_to_f, to perform the conversions. Both of these functions should be defined in a custom module named temps. Custom function c_to_f should be a void function defined to take a Celsius temperature as a parameter. It should calculate and print the equivalent Fahrenheit temperature accurate to three decimal places. Custom function f_to_c should be a value-returning...

  • General overview This program will convert a set of temperatures from Fahrenheit to Celsius and Kelvin....

    General overview This program will convert a set of temperatures from Fahrenheit to Celsius and Kelvin. Your program will be reading in three double values. The first values are starting and ending temperatures. The third value is the increment value. There is no prompt for the input. There is an error message that can be displayed. This is discussed later. You need to display output for all of the values between the starting and ending values. First two values are...

  • C++ requirements All values must be read in as type double and all calculations need to...

    C++ requirements All values must be read in as type double and all calculations need to be done using type double. For part 2 you MUST have at least 4 functions, including main. The main function will be the driver of the program. It needs to either do the processing or delegate the work to other functions. Failure to follow the C++ requirements could reduce the points received from passing the tests. General overview This program will convert a set...

  • Please don't use a void fuction and this is a c++ question. Thanks Write a program...

    Please don't use a void fuction and this is a c++ question. Thanks Write a program that reads two input files whose lines are ordered by a key data field. Your program should merge these two files, writing an output file that contains all lines from both files ordered by the same key field. As an example, if two input files contain student names and grades for a particular class ordered by name, merge the information as shown below Using...

  • //I NEED THE PROGRAM IN C LANGUAGE!// QUESTION: I need you to write a program which...

    //I NEED THE PROGRAM IN C LANGUAGE!// QUESTION: I need you to write a program which manipulates text from an input file using the string library. Your program will accept command line arguments for the input and output file names as well as a list of blacklisted words. There are two major features in this programming: 1. Given an input file with text and a list of words, find and replace every use of these blacklisted words with the string...

  • Temperature Converter Create a temperature conversion program that will convert the following to Fahrenheit: Celsius Kelvin...

    Temperature Converter Create a temperature conversion program that will convert the following to Fahrenheit: Celsius Kelvin Newton Your program should take a character which represents the temperature to convert from and a value to be converted to using the following specification: C - Celsius K - Kelvin N - Newton In addition your program should take in the following character to exit the program: X - eXit the program The numeric input for your program should be of type double....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT