Hi all,
Could you guys help me debug this program? It is supposed to take a temperature scale and temperature value given by the user and convert it into the other two temperature scales. The mechanics of this program seem ok but its just the math part. Please take a look at it and tell me what you guys think:
Code:
#include <iostream.h>
/*
Programmer: Bensan George Benny
Date Due: 05/29/2003
Project 1 - A program that formats an initial temperature value to three temperature standards
*/
// Function Prototypes
double get_far(char user_input, double temp_value);
double get_celsius(char user_input, double temp_value);
double get_kelvin(char user_input, double temp_value);
double main(){
char sentinel = 'y'; // set variable initially to yes to pass doubleo while loop
char user_input; // variable to decide what initial temperature format is
double temp_value; // variable to decide what value it is.
double celsius, fahrenheit, kelvin;
while ((sentinel != 'n')){
cout << "Welcome to Temperature Converter!\n";
cout << "Is starting temperature Celsius, Fahrenheit, or Kelvin?\n";
cout << "Please enter c, f, or k: ";
cin >> user_input;
cout << "Please enter starting temperature: ";
cin >> temp_value;
switch(user_input){
case 'K':
case 'k':
// perform functions
fahrenheit = get_far(user_input, temp_value);
celsius = get_celsius(user_input, temp_value);
kelvin = get_kelvin(user_input, temp_value);
cout << "This temperature is equivalent to the following:\n\n";
cout << fahrenheit << " degrees Fahrenheit\n";
cout << celsius << " degrees Celsius\n";
cout << kelvin << " degrees Kelvin\n\n";
break;
case 'C':
case 'c':
// perform functions
fahrenheit = get_far(user_input, temp_value);
celsius = get_celsius(user_input, temp_value);
kelvin = get_kelvin(user_input, temp_value);
cout << "This temperature is equivalent to the following:\n\n";
cout << fahrenheit << " degrees Fahrenheit\n";
cout << celsius << " degrees Celsius\n";
cout << kelvin << " degrees Kelvin\n\n";
break;
case 'F':
case 'f':
// perform functions
fahrenheit = get_far(user_input, temp_value);
celsius = get_celsius(user_input, temp_value);
kelvin = get_kelvin(user_input, temp_value);
cout << "This temperature is equivalent to the following:\n\n";
cout << fahrenheit << " degrees Fahrenheit\n";
cout << celsius << " degrees Celsius\n";
cout << kelvin << " degrees Kelvin\n\n";
break;
default:
cout << "ERROR: Unknown temperature scale.\n";
break;
}
cout << "Would you like to run it again?(y/n): ";
cin >> sentinel;
}
return 0;
}
/*
Conversion between Celsius and Fahrenheight:
C = 5/9 * (F - 32)
and
F = 9/5 * C + 32
Conversion between Celsius and Kelvin:
C = K - 273.15
*/
// Function Declarations
double get_far(char user_input, double temp_value){
double c, f, k;
switch(user_input){
case 'F':
case 'f':
// find f, given f
f = temp_value;
break;
case 'C':
case 'c':
// find f, given c
c = temp_value;
f = (c + 32)*(9/5);
break;
case 'K':
case 'k':
// find f, given k
k = temp_value;
c = k - 273.15;
f = (c+32)*(9/5);
break;
}
return f;
}
double get_celsius(char user_input, double temp_value){
double c, f, k;
switch(user_input){
case 'C':
case 'c':
c = temp_value;
break;
case 'F':
case 'f':
f = temp_value;
c = (f - 32)*(5/9);
break;
case 'K':
case 'k':
k = temp_value;
c = k - 273.15;
break;
}
return c;
}
double get_kelvin(char user_input, double temp_value){
double c, f, k;
switch(user_input){
case 'K':
case 'k':
// find k, given k
k = temp_value;
break;
case 'C':
case 'c':
// find k, given c
c = temp_value;
k = c + 273.15;
break;
case 'F':
case 'f':
// find k, given f
f = temp_value;
c = (f - 32)*(5/9);
k = c + 273.15;
break;
}
return k;
}