Help! Pass Value & Reference

I need to create a program that is a table to convert between centigrade and fahrenheit temperatures. My solution must must utilize functions to perform the conversions.
Requirements:
1. One of the functions must use pass-by-value, returning the converted
measure
2. One of the functions must use pass-by-reference to store its result (the
function does not have a return value).
Program must create two tables - one showing the centigrade equivalent to
fahrenhrit measures from 0 degrees centigrade to 100 degrees centigrade (by 5
degree increments: 0, 5, 10, . . . , 100) and the other showing fahrenheit
equivalent to centigrade measures 0 through 100 (by 5 degree increments: 0, 5,
10, ... , 100). Original measures are all integer values. Calculated measures are to
be displayed accurate to two decimal places. The output for both tables must fit on
one default screen (78 columns by 22 rows), so your tables will need to be
organized into multiple columns. Everything must be lined up nicely and the tables
neatly and informatively labeled.

Well I got the program to line up correctly, but I can not get the functions to proper calculate.

Where am I going wrong??

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
using std::cout;
using std::endl;

#include <iomanip>
using std::setw;
using std::fixed;
using std::setprecision;

float cent(float); // function prototype
float fahr(float); // function prototype

int _tmain(int argc, _TCHAR* argv[])
{
	// display table 
	cout << "Centigrade to Fahrenheit" << setw(30) 
		<< "Fahrenheit to Centigrade" << endl;

	// create 4 columns
	cout << setw(8) << "Cent" << setw( 14 ) << "Fahr" 
		<< setw(16) << "Fahr" << setw(14) << "Cent" ;

	cout << endl;

	for ( float a = 0; a <=100; a+=5 ) 
		{
			for ( float b = 0; b <= 5; b += 25 )

				cout << setw( 8) << setprecision(0) << a + b << setw( 14 ) 
				<< setprecision(2) << fixed << fahr( a + b ) << ' ' 
				<< setw( 15) << setprecision(0) << a + b << setw( 14 ) 
				<< setprecision(2) << fixed << cent( a + b ) << ' ';

			cout << endl;

			} 

	return 0; 

}  // end main

// function cent
float cent( float f )
{
	return ( ( f - 32 )* 5/9 );

	} // end function cent

// funtion fahr 
float fahr( float c )
{
	return ( 9/5 * (c + 32) );

	} // end function fahr
Last edited on
It is probably a problem with floats, they don't seem to always return correct values or store right...I don't know if doubles are any better, but you could try those.
Last edited on
Note that 5/9 is 0 in the language of C and C++. Since both operands are integers, it expects the result will be an integer, and thus truncates the result to 0. Similarly, 9/5 is 1.

You should instead use 5.0/9.0 and 9.0/5.0
Topic archived. No new replies allowed.