Skip to content

Instantly share code, notes, and snippets.

@jangeador
Created January 12, 2015 15:11
Show Gist options
  • Save jangeador/d77d5f4af7cb73b841c9 to your computer and use it in GitHub Desktop.
Save jangeador/d77d5f4af7cb73b841c9 to your computer and use it in GitHub Desktop.
Temperature Converter (From my early college days!)
/* Delio Castillo
BIS 221 C++
Charles Godfrey, Instructor
Spring 2002
Project Name : Project03A
Purpose of Program
To convert temperatures from Celsius to
Farenheit and viceversa.
Inputs:
"C" to convert from Celsius to Farenheit, or
"F" to convert from Farenheit to Celsius
A temperature
Processes:
Converts the temperature using the formula
Celsius to Farenheit (CF) = F*(9/5)+32
Farenheit to Celsius (!CF) = (C-32)*(5/9)
Outputs:
A converted temperature
Assumptions:
None
*/
#include <iostream>
#include <string>
using namespace std;
void main(){
//Declare and Initialize variables
double temp;
double ConvertTemp;
//This will hold the C or F to know which conversion
//to perform
char theInput;
//Hold the labels for Celsius and Farenheit to
//display the output
string labels[2] = {"Farenheit","Celsius"};
//This variable is true if the conversion
//to be performed is Celsius to Farenheit
bool CF = true;
//If the user enters an invalid value the program will return to this line
InvalidInput:
//Output to the user
cout << "Temperature Converter by Delio Castillo" << endl <<endl;
cout << "Which type of conversion would you like to perform?" << endl;
cout << "(enter 'C' to convert from Celsius to Farenheit, or" << endl;
cout << "'F' to convert from Farenheit to Celsius) ===> ";
//Accept user input
cin >> theInput;
//If the input is not a 'C', 'c', 'F', or 'f' then go back to input
if ((theInput != 'c') && (theInput != 'C')
&& (theInput != 'F') && (theInput != 'f')) goto InvalidInput;
//Accept a temperature input
cout << "Enter the temperature you want to convert ==> ";
cin >> temp;
//If the user wants to convert from Farenheit to Celsius
//Set CF to 0 (False)
if ((theInput == 'F') || (theInput == 'f')) CF = 0;
if (CF){
ConvertTemp = (temp * (9.0/5.0)) + 32;
}
else
{
ConvertTemp = (temp - 32) * (5.0/9.0);
}
cout << temp << " degrees " << labels[CF] << " is equal to "
<< ConvertTemp << " degrees " << labels [!CF] << endl;
}
/* Delio Castillo
BIS 221 C++
Charles Godfrey, Instructor
Spring 2002
Project Name Project03B
Purpose of Program
To convert temperatures from Celsius to
Farenheit and viceversa.
Inputs:
"C" to convert from Celsius to Farenheit, or
"F" to convert from Farenheit to Celsius
A temperature
Processes:
Converts the temperature using the formula
Celsius to Farenheit (CF) = F*(9/5)+32
Farenheit to Celsius (!CF) = (C-32)*(5/9)
Outputs:
A converted temperature
Assumptions:
None
*/
#include <iostream>
#include <string>
using namespace std;
bool CF;
double temp;
double ConvertTemp(double temp, bool CF)
{
//This function accepts two parameters
//a temperature value and an boolean
//value to decide which conversion to perform
if (CF){
return (temp * (9.0/5.0)) + 32;
}
else
{
return (temp - 32) * (5.0/9.0);
}
}
void main(){
//Declare and Initialize variables
//This will hold the C or F to know which conversion
//to perform
char theInput;
//Hold the labels for Celsius and Farenheit to
//display the output
string labels[2] = {"Farenheit", "Celsius"};
//This variable is true if the conversion
//to be performed is Celsius to Farenheit
CF = 1;
//If the user enters an invalid value the program will return to this line
InvalidInput:
//Output to the user
cout << "Temperature Converter by Delio Castillo" << endl <<endl;
cout << "Which type of conversion would you like to perform?" << endl;
cout << "(enter 'C' to convert from Celsius to Farenheit, or" << endl;
cout << "'F' to convert from Farenheit to Celsius) ===> ";
//Accept user input
cin >> theInput;
//If the input is not a 'C', 'c', 'F', or 'f' then go back to input
if ((theInput != 'c') && (theInput != 'C')
&& (theInput != 'F') && (theInput != 'f')) goto InvalidInput;
//Accept a temperature input
cout << "Enter the temperature you want to convert ==> ";
cin >> temp;
//If the user wants to convert from Farenheit to Celsius
//Set CF to 0 (False)
if ((theInput == 'F') || (theInput == 'f')) CF = 0;
cout << temp << " degrees " << labels[CF] << " is equal to "
<< ConvertTemp(temp, CF) << " degrees " << labels [!CF] << endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment