C++ Coding Question 1

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.


  1. !(cin>>cel) it is used to check input validation. cin.clear() is used to clear the buffer.

  2. cel=takeinput() Here the function takeinput() will take the value and return the value that will be stored into cel variable.

  3. 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.

  4. 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.

cin.clear is used to clear the flag.

It will retrieve all the characters until it gets ‘\n’.
Then it will allow taking the input again.