Temp Converter

Hey I am trying to make a temp converter, but I am having some trouble with getting my difference function to work I was wondering if anyone could give me a bit of a hand quite new to this. This is what I have so far.

// Question 0.cpp : Defines the entry point for the console application.
// Includes
#include "stdafx.h"
#include <iostream>
#include <conio.h>
using namespace std;



// Farhenheit Table Function
double Farhenheit(int x)

{
double Farhenheit;

for (int i = 0 ; i <= 100 ; i++)
{
Farhenheit = ( x *9/5 ) +32;
}

return Farhenheit;
}

//Approx Function
double Approx(int x)

{
double Approx;

for (int i = 0 ; i <= 100 ; i++)
{
Approx = (x*2)+30;
}

return Approx;
}

// Differnce Function
int Difference(int x)
{
double Difference;

for (int i = 0 ; i <= 100 ; i++)
{
Difference = Farhenheit-Approx;
}
return Difference;
}

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

cout << "Celcius \t Actual Farhenheit \t Approx \t Difference\n\n";

for ( int x = 0 ; x <= 100 ; x++ )

{
cout << x << " \t\t " << Farhenheit(x) << "\t\t" << Approx(x) << "\t\t" <<Difference << endl;
}

_getch();

return 0;
}
This is wrong on so many levels.
1) Most loops are useless. For example:
1
2
3
for (int i = 0 ; i <= 100 ; i++) {
    Farhenheit = ( x *9/5 ) +32;
}
You are counting the same value 101 times.
2)x*9/5 it is better to use doubles if you are not doing integer division: x*9.0/5.0. It works fine here, but you can run into some unpleasant behavior later.
3) In Difference() you should call Farhenheit and Approx as functions:
Difference = Farhenheit(x)-Approx(x);
Is this Any better?

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
56
57
58
59
60
61
62
63
// firenheight.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <conio.h>
using namespace std;

// Prototypes
double Farhenheit(int x);

// Farhenheit Table Function
double Farhenheit(int x)
{ 
	double farhen;

	{
		farhen = (x*9.0/5.0)+32;
	} 
	return farhen;
}

//Approx Function
double Approx(int x)

{ 
	double Approx;
	{
		Approx = (x*2.0)+30.0;
	}
	
	return Approx;
}

// Differnce Function
double Difference(int x)
{
	double Difference;
	{
		Difference = Farhenheit(x)-Approx(x);
	}
	return Difference;
}

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

	cout << "Celcius \t Actual Farhenheit \t Approx \t Difference\n\n";

	for ( int x = 0 ; x <= 100 ; x++ )

	{
		cout << x << " \t\t " << Farhenheit(x) << "\t\t\t" << Approx(x) << "\t\t" << Difference(x) <<  endl;
	}

	_getch();

	return 0;

	system("pause");
}
Topic archived. No new replies allowed.