Celcius Temperature Table

Oct 5, 2012 at 3:10pm
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
// Includes
#include "stdafx.h"
#include <iostream>
#include <conio.h>
using namespace std;

// Prototypes
double farhenheit();

// Farhenheit Table Function
double farhenheit()
{ 
	int x;
	double conversion;
	
	cout << "F \t C" << endl;
	cout << "_______________" << endl;

	for ( x = 0 ; x <= 20 ; x++ )
	{
		conversion = x * -17.22;
		cout << x << " \t " << endl;
	}

	_getch();

	return conversion;
}
// Main Function
int main()
{
	cout << "This will automatically display a farhenheit to celcius table from 0-20.\n\n";

	farhenheit();

	_getch();

	return 0;
}


Don't worry this isn't homework, im teaching myself c++ more in depth due to the fact i only took a year of it in school.

The exercise from the ebook reads:

The formula for converting a temperature from Farhenheit to Celcius is where F is the Farhenheit temperature and C is the Celcius temperature. Write a function that accepts a farhenheit temperature as and argument. The function should return the temperature, converted to celcius. Demonstrate the function bby calling it in a loop that displays a table of the ferhenheit temperatures 0-20 and their celcius equivalents.

I want to make sure i am doing this right, can anyone look and see please. I have a feeling im not even close to doing this right.
Last edited on Oct 5, 2012 at 3:29pm
Oct 5, 2012 at 3:35pm
In the exercise there is written "Write a function that accepts a farhenheit temperature as and argument. The function should return the temperature, converted to celcius". So you should write a function that accepts a Farhenheit temperature and returns a corresponding Celcius. Your function shall not contain any other code except this calculation.
Last edited on Oct 5, 2012 at 3:36pm
Oct 5, 2012 at 4:49pm
Write a function that accepts a farhenheit temperature as and argument.

Do you know what they mean by argument?

The function declared below does not have an argument.
1
2
// Prototypes
double farhenheit();


This is not correct:
conversion = x * -17.22;

I would move everything that you have in your function (except the return statement) in to main. And have your function just do the proper conversion. Call your function in the loop.
Oct 5, 2012 at 4:53pm
ok thank you
Oct 5, 2012 at 6:01pm
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
// Includes
#include "stdafx.h"
#include <iostream>
#include <conio.h>
using namespace std;

// Prototypes
int celcius(int , int);

// Farhenheit Table Function
int celcius(int x, int conversion)
{ 
	for ( x = 0 ; x <= 20 ; x++)
	{
		conversion = x - 32 * ( 5 / 9 );
	}
	return conversion;
}
// Main Function
int main()
{
	cout << "This will display the farhenheit to celcius conversion table from 0-20.\n\n";

	for ( int x = 0 ; x <= 20 ; x++ )
	{
		cout << x << " \t " << celcius << endl;
	}

	_getch();

	return 0;
}


ok heres my new code everything works except all the celcius temperatures say

013514B0

anyone know why.

Oct 5, 2012 at 6:11pm
That's the address of the celcius function. Perhaps you should actually call the function?
Oct 5, 2012 at 6:12pm
Because that's the address to your function. I take it you're pretty new to programming.

cout << x << " \t " << celcius << endl;

This does not call the function. If you want to a call a function, do this:

foo(arg)
Oct 5, 2012 at 6:27pm
Well im not like new new to programming its just usually i dont do like ex. return conversion, usually i dont do anything like that bu i got to learn it. but thank you
Oct 5, 2012 at 6:33pm
If you aren't familiar with functions, then you're pretty new to programming. They're a fundamental part of programming.
Oct 5, 2012 at 6:51pm
lol i mean the return part confused me, i know functions, just not whena have a return value of like my program its return conversion, usually i do like return 1 or return 0. also usually i dont make any args usually i just user int celcius() or void celcius(), not int celcius (int x, int conversion)
Oct 5, 2012 at 9:00pm
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
// Includes
#include "stdafx.h"
#include <iostream>
#include <conio.h>
using namespace std;

// Prototypes
int celcius(int x);

// Farhenheit Table Function
int celcius(int x)
{ 
	int conversion;

	for ( x = 0 ; x <= 20 ; x++)
	{
		conversion = ( x -32 ) * (5 / 9);
	}
	_getch();

	return conversion;
}
// Main Function
int main()
{
	cout << "This will display the farhenheit to celcius conversion table from 0-20.\n\n";

	cout << "Farhenheit \t Celcius\n\n";

	for ( int x = 0 ; x <= 20 ; x++ )
	{
		cout << x << " \t\t " << celcius(x) << endl;
	}

	_getch();

	return 0;
}


Everything works now, heres my fixed code, but all the conversion numbers are showing up as 0, anyone know why.
Oct 5, 2012 at 9:10pm
Integer division. 5/9 is 0.

conversion = ( x -32 ) * (5.0 / 9);
Oct 5, 2012 at 9:52pm
when i change it to that i get all -6 and a warning saying line 17

warning conversion from double to int, possible loss of data, i even get that warning when i change conversion from int to double.
Oct 6, 2012 at 3:34am
Well, I would expect the warning about the conversion. You can safely ignore it in this case if you don't want the decimal places showing and don't mind a bit of a rounding error.

What is the for loop doing in the celcius function?
Topic archived. No new replies allowed.