| | |
I need some help.
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Nov 2009
Posts: 1
Reputation:
Solved Threads: 0
Could someone please help me figure out what is wrong with my program?
# include <iostream>
# include <string>
using namespace std;
void water_state();
float celsius_to_fahrenheit(float c);
float celsius;
float fahrenheit;
int main()
{
float celsius;
float fahrenheit;
cout << " Enter Temperature in degrees celsius: ";
cin >> celsius;
fahrenheit= celsius_to_fahrenheit (celsius);
}
float celsius_to_fahrenheit(float c)
{
return (((9/5)*c)+23);
}
void water_state()
{
if (fahrenheit <= 212)
return cout << "steam";
else if (fahrenheit>=32)
return cout << "ice";
else
return cout << "water";
}
# include <iostream>
# include <string>
using namespace std;
void water_state();
float celsius_to_fahrenheit(float c);
float celsius;
float fahrenheit;
int main()
{
float celsius;
float fahrenheit;
cout << " Enter Temperature in degrees celsius: ";
cin >> celsius;
fahrenheit= celsius_to_fahrenheit (celsius);
}
float celsius_to_fahrenheit(float c)
{
return (((9/5)*c)+23);
}
void water_state()
{
if (fahrenheit <= 212)
return cout << "steam";
else if (fahrenheit>=32)
return cout << "ice";
else
return cout << "water";
}
0
#2 25 Days Ago
•
•
•
•
Could someone please help me figure out what is wrong with my program?
You declare: float celsius and float fahrenheit twice.. once outside of main(), and once inside of main. One one declaration of the variables are needed. Either before main() or inside of main(), doesn't really make a difference, but only once is needed.
Last edited by Clinton Portis; 25 Days Ago at 12:02 am.
0
#3 25 Days Ago
•
•
•
•
[Only] one declaration of the variables are needed. Either before main() or inside of main(), doesn't really make a difference, but only once is needed.
ukbassman - please use code tags to enclose your code - it makes the code more readable. Like this
[code]
your code goes here
[/code]
I can see several areas you need to look at.
1 - you get wrong results from the conversion function, don't you?
(((9/5)*c)+23) looks OK mathematically, but check your text for explanation of what happens when you divide an integer by an integer. Oh, wait -- +23 ??2. In the water_state( ) function, you have
return cout << "steam"; . This is a void function, so it cannot return any value. And, we generally don't return the value of a cout statement. I think all you want to do here is the cout, omit the return keyword.3. Now that duplicate declaration of the temperature variables is going to rear its ugly head. The instance of fahrenheit that stored the conversion is local to main( ). You don't pass it to the function, so water_state( ) looks to the global variable, which does not have the current value. You should pass the fahrenheit variable from main( ) as parameter to that function.
Fix what you've got, repost it, and tell us specifically what you find to be not working, if you still have problems.
"We Americans got so tired of being thought of as dumb by the rest of the world that we went to the polls last November and removed all doubt."
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
![]() |
Other Threads in the C++ Forum
- Previous Thread: how to set the range using switch statement??
- Next Thread: Calling Functions
| Thread Tools | Search this Thread |
api array based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






