I need some help.

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2009
Posts: 1
Reputation: ukbassman88 is an unknown quantity at this point 
Solved Threads: 0
ukbassman88 ukbassman88 is offline Offline
Newbie Poster

I need some help.

 
-1
  #1
25 Days Ago
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";

}
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 311
Reputation: Clinton Portis is an unknown quantity at this point 
Solved Threads: 31
Clinton Portis's Avatar
Clinton Portis Clinton Portis is offline Offline
Posting Whiz
 
0
  #2
25 Days Ago
Could someone please help me figure out what is wrong with my program?
Is there anything wrong with your program? I don't know. Does the program do something you don't want it to do? Do you have compilation errors? If so, what are they?

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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,675
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is online now Online
Posting Virtuoso
 
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.
Actually, it does make a difference. The declaration outside of main( ) is globally visible and modifiable. This is generally to be avoided, especially in beginning programs, as it may cause the student to develop bad habits that will cause no end of consternation later.

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.
Reply With Quote Quick reply to this message  
Reply

Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC