Here is what I have so far. I understand the middle portion of it. I'm just having a hard time understanding which variables to stick in the voids.

#include <iostream>
#include <iomanip>

using namespace std;

//function prototypes
void getFahrenheit();
void calcCelsius();
void displayCelsius();

int main()
{	
	//declare variables
	int fahrenheit = 0;
	double celsius = 0.0;

Dani AI

Generated

You're almost there. The program in 's post calls the functions in the right order, but the functions either take copies (so changes never reach main) or accidentally read input where they should compute a value. As suggested, use reference parameters for functions that must modify main's variables. As pointed out, remove the stray cin >> celsius; in the conversion function — that overwrote the computed result.

A minimal, focused correction (showing only the signatures and the conversion body) looks like this:

void getFahrenheit(int &fahrenheit);
void calcCelsius(int fahrenheit, double &celsius);
void displayCelsius(double celsius);

void calcCelsius(int fahrenheit, double &celsius)
{
    celsius = (fahrenheit - 32.0) * 5.0 / 9.0; // use floating-point literals
}

Practical checklist to fix the program:

  • Change prototypes and definitions to match exactly (include & where you intend references).
  • In getFahrenheit, read into the referenced variable (cin >> fahrenheit;).
  • Remove any cin >> celsius; from calcCelsius; compute and assign to the referenced celsius.
  • Use 5.0/9.0 (not 5/9) so you get floating-point division.
  • Format output in displayCelsius (use fixed/setprecision) but remember setprecision(0) rounds to whole numbers.
  • Test with known values (32 F -> 0 C, 212 F -> 100 C).

Optional: if you prefer clearer data flow, make calcCelsius return a double instead of using a reference.

Recommended Answers

All 4 Replies

Here is what I have so far. I understand the middle portion of it. I'm just having a hard time understanding which variables to stick in the voids.

#include <iostream>
#include <iomanip>

using namespace std;

//function prototypes
void getFahrenheit();
void calcCelsius();
void displayCelsius();

int main()
{	
	//declare variables
	int fahrenheit = 0;
	double celsius = 0.0;

What are you getting from fahrenheit()? Put it in there as a reference argument/parameter...

Same with calcCelsius(), except you will need 2 arguments/parameters. One will have to be a reference, the other can be a value pass or a reference, it doesn't really matter. I'll leave it to you to decide which is which.

For displayCelsius() all that you have to do is send the celsius value. You don't need a reference because you aren't trying to modify it.

#include <iostream>
#include <iomanip>

using namespace std;

//function prototypes
	void getFahrenheit(int); 
	void calcCelsius(int, double);
	void displayCelsius(double);

int main()
{	
	//declare variables
	int fahrenheit = 0;
	double celsius = 0.0;
	
	//get input item
	getFahrenheit(fahrenheit);

	//calculate Celsius
	calcCelsius(fahrenheit, celsius);

	//display output item
	cout << fixed << setprecision(0);
	displayCelsius(celsius);

    return 0;
}   //end of main function

//*****function definitions*****

void getFahrenheit(int fahrenheit)
{
cout << "Enter Degrees in Fahrenheit: ";
cin >> fahrenheit;
} //end of getFahrenheit function

void calcCelsius (int fahrenheit, double celsius)
{
celsius = ((5.0/9.0)* (fahrenheit - 32.0));
cin >> celsius;
} //end of calcCelsius function

void displayCelsius(double celsius)
{
cout << fixed << setprecision(0);
cout << "Temperature in Celsius is: " << celsius << endl;
}

I tried, but it does not seem to be working. There is no error. The code is just messed up.

remove cin >> celsius; in calcCelsius.

Edit: You should use reference instead of just copying the values:

void getFahrenheit(int); // Here you copy an object from main and read a value into the copied(not the same) object.
void getFahrenheit(int&); // Here you refer to an object from main and read a value into it.
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.