Data Conversion Program Write a program that reads data from a file, performs a
ID: 3820949 • Letter: D
Question
Data Conversion Program
Write a program that reads data from a file, performs a units conversion, and writes the data to a new file.
Offer the user the following choices of conversions: degrees F to degrees C, degrees C to degrees F, feet to meters, meters to feet, ounces to grams, grams to ounces.
In the new file, the first line of the file should indicate the original units and the altered units, and the next lines should have both the input and the converted data.
for example:
INPUT:
32.0
0
72
OUTPUT:
degrees F degrees C
32 0
0 -17.78
72 22.22
------------------------------------------------------------------------------------------------------------
//There have some errors, please check it and change it whatever you want to be right answer - thank you so much for helping
// Conversion Program
#include
#include
#include
using namespace std;
int main (int argc, char *argv[])
{
ifstream in_stream;
ofstream out_stream;
//open the file
in_stream.open("input.txt");
//open output file
out_stream.open("output.txt");
string data;
int ch;
cout <<"Enter your choice for data conversion ";
cout <<" 1.degrees F to degrees C";
cout <<" 2.degrees C to degrees F";
cout <<" 3.feet to meters";
cout <<" 4.meters to feet";
cout <<" 5.ounces to grams";
cout <<" 6.grams to ounces ";
cin >> ch;
int n;
double out;
switch(ch)
{
case 1:
out_stream << "degrees F degrees C";
break;
case 2: out_stream << "degrees C degrees F";
break;
case 3: out_stream << "feet meters";
break;
case 4: out_stream << "meters feet";
break;
case 5: out_stream << "ounces grams";
break;
case 6: out_stream << "grams ounces";
break;
}
out_stream <<" ";
//loop till end of the file
while(!in_stream.eof())
{
//read the file data
getline(in_stream,data);
stringstream(data) >> n;
out_stream << data <<" ";
switch(ch)
{
case 1:
out=(n-32)/1.8;
break;
case 2: out=(n*1.8)+32;
break;
case 3: out=(n/3.28);
break;
case 4: out=(n*3.28);
break;
case 5: out=(n/0.035);
break;
case 6: out=(n*0.035);
break;
}
out_stream << out;
out_stream <<" ";
}
//close the stream
in_stream.close();
out_stream.close();
}
Explanation / Answer
Hi,
I have edited the given code and comments are placed wherever are required.
Please find the code below:-
CODE:-
===================================================================================
#include <iostream>
#include <fstream>
#include <conio.h>
using namespace std;
int main (int argc, char *argv[])
{
ifstream in_stream;
ofstream out_stream;
//open the file
in_stream.open("input.txt");
//open output file
out_stream.open("output.txt");
string data;
int ch;
//This array will store the data from the file.
double conv[] = {0.0,0.0,0.0}; //0 for first line, 1 for second,2 for third
//loop till end of the file and get the data in array conv[]
for(int i = 0; i < 3; i++)
{
if(!in_stream.eof())
{
getline(in_stream,data); //Get 1 line from the file and place into data
conv[i] = atod(data.c_str()); //set the conv variable to the double version of the line
}
}
//Ask the user to enter their choice
cout <<"Enter your choice for data conversion ";
cout <<" Enter 1 for degrees F to degrees C";
cout <<" Enter 2 for degrees C to degrees F";
cout <<" Enter 3 for feet to meters";
cout <<" Enter 4 for meters to feet";
cout <<" Enter 5 for ounces to grams";
cout <<" Enter 6 for grams to ounces ";
cin >> ch;
double out;
//use switch case ,when selected case loop through whole cov[] array and convert the data as oer requirement
//and then print it to ouput file.
switch(ch)
{
case 1:
out_stream << "degrees F degrees C"<<" ";
for (int j=0;j<3;j++)
{
out=(conv[j]-32)/1.8;
out_stream <<conv[j]<<" "<<out<<" ";
}
break;
case 2: out_stream << "degrees C degrees F"<<" ";
for (int j=0;j<3;j++)
{
out=(conv[j]*1.8)+32;
out_stream <<conv[j]<<" "<<out<<" ";
}
break;
case 3: out_stream << "feet meters"<<" ";
for (int j=0;j<3;j++)
{
out=(conv[j]/3.28);
out_stream <<conv[j]<<" "<<out<<" ";
}
break;
case 4: out_stream << "meters feet"<<" ";
for (int j=0;j<3;j++)
{
out=(conv[j]*3.28);
out_stream <<conv[j]<<" "<<out<<" ";
}
break;
case 5: out_stream << "ounces grams"<<" ";
for (int j=0;j<3;j++)
{
out=(conv[j]/0.035);
out_stream <<conv[j]<<" "<<out<<" ";
}
break;
case 6: out_stream << "grams ounces"<<" ";
for (int j=0;j<3;j++)
{
out=(conv[j]*0.035);
out_stream <<conv[j]<<" "<<out<<" ";
}
break;
}
//close the streams
in_stream.close();
out_stream.close();
}
=================================================================================
please let me know for clarification.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.