Write a program that will read an input file and write an output file. Each line
ID: 3881990 • Letter: W
Question
Write a program that will read an input file and write an output file. Each line of the input file contains two values, the legs of a right triangle. The program should calculate the hypotenuse for each triangle, and write all three sides to the output file. The output file should be in the format shown. Sample Run Input file: Input.txt Output file: Output.txt Input.txt Output.txt 1 4.5 10 3.1415 3.5 8.25 6.1 2 3.000 4.000 12.000 1.000 4.500 4.500 10.000 3.141 31.415 3.500 8.250 28.875 6.1002.000 12.200 5 pairs read Print a copy of your source code, the display output, and the output file. Your code will be graded on comments and functionality.Explanation / Answer
PLEASE REFER BELOW CODE
#include<iostream>
#include<fstream>
#include<string>
#include<iomanip>
using namespace std;
int main()
{
ifstream infile; //create stream for input file
ofstream outfile; //create stream for output file
string file_name; //string for file name
double a,b;
int count_arg = 0;
cout<<"Input file : "; //taking input file name
cin>>file_name;
infile.open(file_name.c_str()); //opening input file name
cout<<"Output file : "; //taking output file name
cin>>file_name;
outfile.open(file_name.c_str()); //create opening output file
if(infile.is_open()) //if input file is presnt then read
{
while(infile >> a >> b)
{
count_arg++;
outfile<<fixed<<setprecision(3)<<a<<" "<<b<<" "<<(a * b)<<endl;
}
}
else //if input file is not present
{
cout<<"Error in opening input file"<<endl;
return 0;
}
cout<<count_arg<<" pairs read"<<endl;
infile.close();
outfile.close();
return 0;
}
PLEASE REFER BELOW OUTPUT
khushal@khushal-ubuntu:~/Desktop/Chegg$ g++ -g Input_output_file.cpp -o ipfile
khushal@khushal-ubuntu:~/Desktop/Chegg$ ./ipfile
Input file : Input.txt
Output file : Output.txt
5 pairs read
khushal@khushal-ubuntu:~/Desktop/Chegg$
BELOW ARE THE FILES
Input.txt
3 4
1 4.5
10 3.1415
3.5 8.25
6.1 2
Output.txt
3.000 4.000 12.000
1.000 4.500 4.500
10.000 3.142 31.415
3.500 8.250 28.875
6.100 2.000 12.200
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.