Write a program that has the main() call a user-defined function that takes a Celcius temperature value as an argument and then returns the equivalent Fahrenheit value. The program should request the Celcius value as input from the user and display the result as show the following code:
Please enter a Celcius value: 20
20 degrees celsius is 68 degrees Fahrenheit.
C
C
#include <iostream>
using namespace std;
int takeinput()
{
int cel;
cout<<“Please enter a celsious value: “;
while(!(cin>>cel))
{
cin.clear();
while(cin.get()!=’\n’)
continue;
cout<<“Please enter a Celsious value:”;
}
return cel;
}
int tem(int cel)
{
return 1.8*cel+32.0;
}
int main()
{
int cel, far;
cel=takeinput();
far=tem(cel);
cout<<cel<<” degrees celsious is “<<far<<” degrees Fahrenheit”<<“\n”;
return 0;
}
Remember
!(cin>>cel) it is used to check input validation. cin.clear() is used to clear the buffer.
cel=takeinput() Here the function takeinput() will take the value and return the value that will be stored into cel variable.
far=tem(cel) Here tem is the function name that takes cel as an argument. And after concerting Celsius into Fahrenheit value, it returns and stores to far variable.
Here the while loop is used for input validation checking. cel variable is used for storing double values. Our target is to avoid the wrong type of input.
If the user inputs a string here, then !(cin>>cel) will return true value while loop inner statement will be executed then.