#include <iostream>	
using namespace std;


int CelsiustoFahrenheit()
{
   
	
float celsius;
    cin >> celsius;
    
float fahrenheittemp;
    fahrenheittemp =  celsius * 1.8 +32;
    
   
   
};




int FahrenheittoCelsius()
{
   
float celsiustemp;	
float fahrenheittemp;
 cin >> fahrenheittemp;
celsiustemp =  (fahrenheittemp-32)/1.8 ;
 
};


int main()
{
	int choice;
	float celsiustemp,fahrenheittemp; 
	cout << "1.Celsius to Fahrenheit" << endl;
	cout << "2.Fahrenheit to Celsius" << endl;
	cout << "Choose between 1 & 2 : " << endl;
	cin>>choice;
	if (choice==1)
		{
			cout << "Enter the temperature in Celsius : " << endl;
			cin>>celsiustemp;
			fahrenheittemp=(1.8*celsiustemp)+32;
			cout << "Temperature in Fahrenheit = " << FahrenheittoCelsius()<< endl;

			return 0;
		}
		
		else
				
		{
			cout << "Enter the temperature in Fahrenheit : " << endl;
			cin>>fahrenheittemp;
			celsiustemp=(fahrenheittemp-32)/1.8;
			cout << "Temperature in Celsius = " << CelsiustoFahrenheit() << endl;

			return 0;
		}


	
}

Dani AI

Generated

C4716 is raised when a non-void function can finish without returning a value. In your case both conversion functions are declared to return an int, but neither ends with a return, so the compiler cannot see a value coming back on every path. There is also a logic mix-up: when the user picks option 1 (Celsius to Fahrenheit) you stream the result of the Fahrenheit-to-Celsius function into cout, and vice versa. As noted, temperatures are not integers, so prefer double over int.

A simple fix is to keep I/O in main and make the conversion routines pure functions that take an argument and return a value. That removes the warning entirely and makes the code easier to test and reuse.

double to_fahrenheit(double c) { return c * 9.0 / 5.0 + 32.0; }
double to_celsius(double f)    { return (f - 32.0) * 5.0 / 9.0; }

Use them like ordinary math: read the number, call the right function, and print the returned value. That directly addresses C4716 because every control path in each function returns. It also avoids mixing input with calculation, which is what was steering you toward with passing parameters.

A few quick tips:

  • Make sure the branch for choice==1 prints to_fahrenheit(celsius) and the other branch prints to_celsius(fahrenheit).
  • Write the constants as 9.0/5.0 and 5.0/9.0 to guarantee floating-point math (no accidental integer division).
  • If you really do not want to return a value, change the function return type to void and print inside the function, but then do not stream the function call into cout because there is no value to insert.
  • Sanity-check with known pairs: 0 C -> 32 F, 100 C -> 212 F, 98.6 F -> 37 C. As showed, the key is that every non-void function must explicitly return.

Recommended Answers

All 4 Replies

When posting code always use code-tags!

You have two functions that should return a value, but don't: int CelsiustoFahrenheit() and int FahrenheittoCelsius() After calculating the values you'll have to return them from the function, because it makes no sense in calculating them if you're not going to use them right?
So in the first function add return fahrenheittemp; and in the second return celsiustemp; Why are you using an int anyway? Temperatures aren't always integer values you know.

// example
int func(void)
{
   int i=0;
   return i; // return value
}

You didn't want to try out the Kelvin one I posted, too?

Your functions should except parameters too:

double cubeit(double value)
{
  return(value * value * value);
}

use it like:

double x;
cin >> x;
x = cubeit(x);
cout << x << endl;

Thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.