C++ Primer Plus Chapter 2 Exercise 4

c plus plusExercise 4 is slightly more involving, but provides all the information we need to convert temperatures.

4. Write a program that has main() call a user-defined function that takes a Celsius temperature
value as an argument and then returns the equivalent Fahrenheit value. The program
should request the Celsius value as input from the user and display the result, as
shown in the following code:
Please enter a Celsius value: 20
20 degrees Celsius is 68 degrees Fahrenheit.
For reference, here is the formula for making the conversion:
Fahrenheit = 1.8 × degrees Celsius + 32.0

 #include <iostream>

using namespace std;

float convert(float);
float celsius;

int main()
{
cout << "Please enter a celsius temperature: ";
cin >> celsius;

cout << celsius << " degrees celsius is " << convert(celsius) << " degrees fahrenheit" << endl;
cin.ignore();

return 0;
}

float convert(float celsius)
{
return (celsius * 1.8) + 32.0;
}
 
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s