### 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:
 ``123`` ``````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?

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263`` ``````// firenheight.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include #include 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.