Programming Fundamentals/Conditions/C++: Difference between revisions
Jump to navigation
Jump to search
Content deleted Content added
Creating |
(No difference)
|
Revision as of 00:16, 2 February 2017
subroutines.cpp
// This program asks the user to select Fahrenheit or Celsius conversion
// and input a given temperature. Then the program converts the given
// temperature and displays the result.
#include <iostream>
using namespace std;
void toCelsius();
void toFahrenheit();
int main()
{
string choice;
cout << "Enter F to convert to Fahrenheit or C to convert to Celsius:" << endl;
cin >> choice;
if (choice == string("C") || choice == string("c"))
{
toCelsius();
}
else if (choice == string("F") || choice == string("f"))
{
toFahrenheit();
}
else
{
cout << "You must enter C to convert to Celsius or F to convert to Fahrenheit!" << endl;
}
}
void toCelsius()
{
double f;
double c;
cout << "Enter Fahrenheit temperature:" << endl;
cin >> f;
c = (f - 32) * 5 / 9;
cout << f << "° Fahrenheit is " << c << "° Celsius" << endl;
}
void toFahrenheit()
{
double c;
double f;
cout << "Enter Celsius temperature:" << endl;
cin >> c;
f = c * 9 / 5 + 32;
cout << c << "° Celsius is " << f << "° Fahrenheit" << endl;
}
Try It
Copy and paste the code above into one of the following free online development environments or use your own C++ compiler / interpreter / IDE.