| View previous topic :: View next topic |
| Author |
Message |
AndrewMan Grandmaster Cheater Supreme
Reputation: 0
Joined: 01 Aug 2007 Posts: 1257
|
Posted: Fri Jul 10, 2009 5:25 pm Post subject: Celsius to Fahrenheight converter error |
|
|
| Code: | //Celsius to Fahrenheit July 10, 2009
#include <iostream>
using namespace std;
int Celsius;
int Fahrenheight(int);
int main()
{
cout << "Please enter a Celsius value: ";
cin >> Celsius;
int degree = Fahrenheight(degree);
cout << Celsius << " degrees Celsius is " << degree << " degrees Fahrenheit";
cin.get();
cin.get();
return 0;
}
int Fahrenheight(int degree)
{
return degree = 1.8 * Celsius + 32;
} |
I get this error after I type an integer into
Run-Time Check Failure #3 - The variable 'degree' is being used without being initialized.
It gives me the option to hit Continue, or Break, and when I hit Continue the code continues on and the program gives me a Fahrenheight value. I dunno why I am getting this error. I am using VC++.
_________________
|
|
| Back to top |
|
 |
HolyBlah Master Cheater
Reputation: 2
Joined: 24 Aug 2007 Posts: 446
|
Posted: Fri Jul 10, 2009 5:42 pm Post subject: |
|
|
Change | Code: | | int degree = Fahrenheight(degree); | to | Code: | | int degree = Fahrenheight(Celsius); |
|
|
| Back to top |
|
 |
AndrewMan Grandmaster Cheater Supreme
Reputation: 0
Joined: 01 Aug 2007 Posts: 1257
|
Posted: Fri Jul 10, 2009 5:45 pm Post subject: |
|
|
| HolyBlah wrote: | Change | Code: | | int degree = Fahrenheight(degree); | to | Code: | | int degree = Fahrenheight(Celsius); |
|
Ah, thanks dude
_________________
|
|
| Back to top |
|
 |
hcavolsdsadgadsg I'm a spammer
Reputation: 26
Joined: 11 Jun 2007 Posts: 5801
|
Posted: Fri Jul 10, 2009 10:39 pm Post subject: |
|
|
It will actually work fine like that, despite the error, because the "Fahrenheight" function doesn't even use it's argument.
| Code: | int Fahrenheight(int degree)
{
return degree = 1.8 * Celsius + 32; //degree is nowhere to be found
} |
| Code: | | int degree = Fahrenheight(0); | for example will also get rid of the warning and work fine.
Even more fun is the type conversion warning your code will throw.
| Quote: | | warning C4244: '=' : conversion from 'double' to 'int', possible loss of data |
1.8 is going to be considered a double.
1.8f is going to be seen as a float.
You're trying to return an int though, so the compiler will likely just convert it on it's own, but give you the warning you deserve.
|
|
| Back to top |
|
 |
|