Hi I made a temperature conversion and the code is kinda many so i want to reduce it any idea how can i do it? tnx in advance. Here's the code..
Code:
#include <iostream>
#include <conio.h>
#include <stdlib.h>
using namespace std;
main(){
float ctemp, ftemp, ktemp, rtemp, c, f, k, r;
int ch;
char con, num;
do{
cout<<"1. Celsius\n" <<"2. Fahrenheit\n" <<"3. Kelvin\n"<<"4. Rankine\n";
cout<<"Choice: ";
cin>> ch;
system("cls");
switch(ch){
case 1: cout <<"Enter Temperature in Celsius: ";
cin>> ctemp;
cout<<"Convert to:?\n";
cout<<"a. Fahrenheit\n" <<"b. Kelvin\n" <<"c. Rankine\n";
cout<<"Choice: ";
cin>> con;
switch(con){
case 'a': f = (1.8*ctemp) + 32;
cout << "Temperature in Fahrenheit = " << f << endl; break;
case 'b': k = ctemp + 273.15;
cout << "Temperature in Kelvin = " << k << endl; break;
case 'c': r = (ctemp + 273.15) * 1.8;
cout << "Temperature in Rankine = " << r << endl; break;
default: cout<<"Wrong choice exiting...\n"; exit(1);break;
}
break;
case 2:
cout <<"Enter Temperature in Fahrenheit: ";
cin>> ftemp;
cout<<"Convert to:?\n";
cout<<"a. Celsius\n" <<"b. Kelvin\n" <<"c. Rankine\n";
cout<<"Choice: ";
cin>> con;
switch(con){
case 'a': c = (ftemp - 32) * 0.56;
cout << "Temperature in Celsius = " << c << endl;
break;
case 'b': k = 273 + ((ftemp - 32.0) * (5.0/9.0));
cout << "Temperature in Kelvin = " << k << endl;
break;
case 'c': r = ftemp + 460;
cout << "Temperature in Rankine = " << r << endl;
break;
default: cout<<"Wrong choice exiting...\n"; exit(1);
break;
}
break;
case 3:
cout <<"Enter Temperature in Kelvin: ";
cin>> ktemp;
cout<<"Convert to:?\n";
cout<<"a. Celsius\n" <<"b. Fahrenheit\n" <<"c. Rankine\n";
cout<<"Choice: ";
cin>> con;
switch(con){
case 'a': c = ktemp - 273.15;
cout << "Temperature in Celsius = " << c << endl;
break;
case 'b': f = ((ktemp - 273.15) * (1.8)) + 32;
cout << "Temperature in Fahrenheit = " << f << endl;
break;
case 'c': r = ktemp * 1.8;
cout << "Temperature in Rankine = " << r << endl;
break;
default: cout<<"Wrong choice exiting...\n"; exit(1);
break;
}
break;
case 4:
cout <<"Enter Temperature in Rankine: ";
cin>> rtemp;
cout<<"Convert to:?\n";
cout<<"a. Celsius\n" <<"b. Fahrenheit\n" <<"c. Kelvin\n";
cout<<"Choice: ";
cin>> con;
switch(con){
case 'a': c = (rtemp / 1.8) - 273.15;
cout << "Temperature in Celsius = " << c << endl;
break;
case 'b': f = rtemp - 460;
cout << "Temperature in Fahrenheit = " << f << endl;
break;
case 'c': r = ktemp * 1.8;
cout << "Temperature in Kelvin = " << r << endl;
break;
default: cout<<"Wrong choice exiting...\n"; exit(1);
break;
}
}
cout<<"Do you want to continue (y/n): ";
cin>> num;
}while(num=='y');
getch();
}
and btw what's the formula in converting rankine to kelvin i have already searched in the internet and found nothing.. lol i don't even know if my formulas area correct.