In: Computer Science
Write a program that prints a custom conversion table from Celsius temperatures to Fahrenheit and Newton (Links to an external site.) temperatures. The formula for the conversion from Celsius to Fahrenheit is :
F=9/5*C+32
F is the Fahrenheit temperature, and C is the Celsius temperature.
The formula for the conversion from Celsius to Newton is
C = 100/33*N
N is the Newton Temperature and C is the Celsius temperature
Your C++ program should prompt the user for a lower value and upper value for a range of temperatures in Celsius. It should then prompt the user for the amount they want to increment by. Then use a loop to output to a file named conversion_table.txt a table of the Celsius temperatures and their Fahrenheit and Newton equivalents within the range of values using the increment given by the user. Make sure to format your output to 2 decimal places.
INPUT VALIDATION: Ensure the second number is greater than the first number, and make sure the increment is greater than 0.
PLEASE EXPLAIN. USE DOCUMENTATION.
Code:
#include<iostream>
//as we are using files we have to use this library
#include<fstream>
//as we are using fixed<<serprecision
#include<iomanip>
using namespace std;
int main(){
//declaring variables
float lower,upper,increment;
//celsius,fareheit,newton
float C,F,N;
//file object
ofstream file("conversion_table.txt");
//taking inputs from user
cout<<"Enter lower boundary:
"<<endl;
cin>>lower;
cout<<"Enter upper boundary:
"<<endl;
cin>>upper;
//input validation
//if upper boundary is lessthan lower we will promt
user to enter
//upper boundary until the user enters it
correctly
while(upper<lower){
cout<<"Upper boundary is
lessthan Lower, Enter upper boundary again: "<<endl;
cin>>upper;
}
cout<<"Enter increment: "<<endl;
cin>>increment;
//input validation
//if the user enters increment as 0 or negative, we
will prompt
//until he enters a positive
while(increment<0){
cout<<"Increment shouldnt be
negative or zero, enter again: "<<endl;
cin>>increment;
}
//writing upperboundary,lower boundary and increment
to file
file<<"The Upper Boundary is:
"<<upper<<endl;
file<<"The Lower Boundary is:
"<<lower<<endl;
file<<"The Increment is:
"<<increment<<endl;
file<<"|---------------|---------------|---------------|----"<<endl;
file<<"|Celsius\t|Farenheit\t|Newton\t\t|"<<endl;
file<<"|---------------|---------------|---------------|----"<<endl;
for(float i=lower;i<=upper;i=i+increment){
//calculating farenheit and newton
from celsius
C=i;
F=(9.0/5.0)*C+32;
N = (33.0/100.0)*C;
//printing 2 decimal places
file<<"|"<<fixed<<setprecision(2)<<C<<"\t\t|"<<F<<"\t\t|"<<N<<"\t\t|"<<endl;
}
//closing file
file.close();
}
Output:
Code Screenshot:
Code Snippet:
#include<iostream>
//as we are using files we have to use this library
#include<fstream>
//as we are using fixed<<serprecision
#include<iomanip>
using namespace std;
int main(){
//declaring variables
float lower,upper,increment;
//celsius,fareheit,newton
float C,F,N;
//file object
ofstream file("conversion_table.txt");
//taking inputs from user
cout<<"Enter lower boundary: "<<endl;
cin>>lower;
cout<<"Enter upper boundary: "<<endl;
cin>>upper;
//input validation
//if upper boundary is lessthan lower we will promt user to enter
//upper boundary until the user enters it correctly
while(upper<lower){
cout<<"Upper boundary is lessthan Lower, Enter upper boundary again: "<<endl;
cin>>upper;
}
cout<<"Enter increment: "<<endl;
cin>>increment;
//input validation
//if the user enters increment as 0 or negative, we will prompt
//until he enters a positive
while(increment<0){
cout<<"Increment shouldnt be negative or zero, enter again: "<<endl;
cin>>increment;
}
//writing upperboundary,lower boundary and increment to file
file<<"The Upper Boundary is: "<<upper<<endl;
file<<"The Lower Boundary is: "<<lower<<endl;
file<<"The Increment is: "<<increment<<endl;
file<<"|---------------|---------------|---------------|----"<<endl;
file<<"|Celsius\t|Farenheit\t|Newton\t\t|"<<endl;
file<<"|---------------|---------------|---------------|----"<<endl;
for(float i=lower;i<=upper;i=i+increment){
//calculating farenheit and newton from celsius
C=i;
F=(9.0/5.0)*C+32;
N = (33.0/100.0)*C;
//printing 2 decimal places
file<<"|"<<fixed<<setprecision(2)<<C<<"\t\t|"<<F<<"\t\t|"<<N<<"\t\t|"<<endl;
}
//closing file
file.close();
}