-
user-defined functions
Hi all. I'm writing a program to convert degrees F to degrees C by calling user-defined functions. I'm trying to get the hang of it, but I'm definately doing something wrong here. Suggestions??
Code:
#include <iostream>
using namespace std;
int GetFahrenheit();
double ComputeCentigrade();
double DisplayCentigrade();
int F;
int main()
{
cout << "Enter temperature in Fahrenheit: ";
cin >> F;
cout << endl;
cout << "Current temperature = " << GetFahrenheit() << endl;
cout << "Current temperature = " << DisplayCentigrade() << endl;
return 0;
}
int GetFahrenheit()
{
int F;
return F;
}
double ComputeCentigrade()
{
double C;
int F;
C = (5/9) * (F-32);
return C;
}
double DisplayCentigrade()
{
double C;
return C;
}
-
What are you trying to do here exactly with these functions? You don't pass anything to them. You don't do anything in them. All you're doing is calling a function wich returns a local value. The value you're returning hasn't actually been initialized to anything either, so it's just some random-ish value.
If you're trying to use the C and / or F values you've declared in main, then you need to be passing those over to the function you want to use them in.
Quzah.
-
Sorry, like I said... I'm new to this. Is this getting closer? I'm still getting build errors.
Code:
#include <iostream>
using namespace std;
int GetFahrenheit(int);
double ComputeCentigrade(int, double);
double DisplayCentigrade(double);
int F;
double C;
int main()
{
cout << "Enter temperature in Fahrenheit: ";
cin >> F;
cout << endl;
cout << "Current temperature = " << GetFahrenheit(F) << endl;
cout << "Current temperature = " << DisplayCentigrade(C) << endl;
return 0;
}
int GetFahrenheit(int F)
{
return F;
}
double ComputeCentigrade(int F, double C)
{
C = (5/9) * (F-32);
return C;
}
double DisplayCentigrade(double C)
{
return C;
}
-
I think you're getting functions confused with class methods... this is how a simple function would look:
Code:
int foo(int in);
int main()
{
int number=1;
number=foo(1);
return 0;
}
int foo(int in)
{
return in+1;
}
generally, you want to avoid declaring your variables globally, or in this case, outside of main().
-
The only thing I'm having trouble with now is getting the degrees C to come out correctly. Help?
Code:
#include <iostream>
using namespace std;
int GetFahrenheit(int);
double ComputeCentigrade(int);
double DisplayCentigrade(double);
int main()
{
int F;
double C;
cout << "Enter temperature in Fahrenheit: ";
cin >> F;
cout << endl;
cout << "Current temperature = " << GetFahrenheit(F) << endl;
cout << "Current temperature = " << DisplayCentigrade(C) << endl;
return 0;
}
int GetFahrenheit(int F)
{
return F;
}
double ComputeCentigrade(int F)
{
double C;
C = (5/9) * (F-32);
return C;
}
double DisplayCentigrade(double C)
{
return C;
}
-
well, clean up the mess first and you'll realize what's wrong:
Code:
#include <iostream>
using namespace std;
double ComputeCentigrade(int);
int main()
{
int F;
double C;
cout << "Enter temperature in Fahrenheit: ";
cin >> F;
cout << endl;
cout << "Current temperature = " << F << endl;
cout << "Current temperature = " << C << endl;
return 0;
}
double ComputeCentigrade(int F)
{
double C;
C = (5/9) * (F-32);
return C;
}
you don't have to create a function to output a variable. for example, what you were doing was sending F to a function, which did nothing but return it, unchanged. it really didn't do anything useful.
-
Code:
#include <iostream>
using namespace std;
double ComputeCentigrade(int);
int main()
{
int F;
cout << "Enter temperature in Fahrenheit: ";
cin >> F;
cout << endl;
cout << "Current temperature = " << F << endl;
cout << "Current temperature = " << ComputeCentigrade(F) << endl;
return 0;
}
double ComputeCentigrade(int F)
{
return ((5.0f/9.0f)*(F-32));
}
[edit]
Sorry didnt see smalls post ^^^
[/edit]
-
I figured that would be easier. But I'm supposed to be able to use those three steps... baby steps so that I understand each part better. Any suggestions on how to clean things up while still using those three functions? :confused:
-
well, if you really want to use those other functions, go ahead... just everyplace in main() that you see C, replace with DisplayCentigrade(C), and everyplace you see F, replace with GetFahrenheit(F), except for where you declare them and assign their values... (i.e. in the cout statements only)
even though that's not really baby steps... that's just bad programming practice...
-
OK... well in that case, do you know what I'm still doing wrong with this longer way of writing it out? When I execute, the temp in C comes out weird. :confused:
Code:
#include <iostream>
using namespace std;
int GetFahrenheit(int);
double ComputeCentigrade(int);
double DisplayCentigrade(double);
int main()
{
int F;
double C;
cout << "Enter temperature in Fahrenheit: ";
cin >> F;
cout << endl;
cout << "Current temperature = " << F << "F" << endl;
cout << "Current temperature = " << C << "C" << endl;
return 0;
}
int GetFahrenheit(int F)
{
return F;
}
double ComputeCentigrade(int F)
{
double C;
C = (5/9) * (F-32);
return C;
}
double DisplayCentigrade(double C)
{
return C;
}
-
that's because the C you're outputting has never been initialized to anything. in the code you just posted, you're never calling any of the functions.
-
Code:
C = (5/9) * (F-32);
-->
Code:
C = (5.0/9.0) * (F-32);
-
Why do this:
Code:
double ComputeCentigrade(int F)
{
double C;
C = (5.0/9.0) * (F-32);
return C;
}
When you could do this:
Code:
double ComputeCentigrade(int F)
{
return ((5.0/9.0)*(F-32));
}
-
I made some more changes, based on what you all have suggested. I'm still confused about something though... how do you "call" a function? Here's my code... it works but the centigrade degree doesn't come out right, and when I compile it, I don't know what to do about the error.
Code:
#include <iostream>
using namespace std;
int GetFahrenheit(int);
double ComputeCentigrade(int);
double DisplayCentigrade(double);
int main()
{
int F;
double C;
cout << "Enter temperature in Fahrenheit: ";
cin >> F;
cout << endl;
cout << "Current temperature = " << GetFahrenheit(F) << "F" << endl;
cout << "Current temperature = " << DisplayCentigrade(C) << "C" << endl;
return 0;
}
int GetFahrenheit(int F)
{
return F;
}
double ComputeCentigrade(int F)
{
return (5.0 / 9.0) * (F - 32);
}
double DisplayCentigrade(double C)
{
int F;
C = (5.0 / 9.0) * (F - 32);
return C;
}
-
That's an example of calling a function.
What is the error you're getting?
The centigrade degree isn't coming out right because when you call the DisplayCentigrade function, you're passing it C, which hasn't been defined. In that same function, you use F to make a calculation. F hasn't been defined in your function either.