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";

}

Recommended Answers

All 2 Replies

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.

[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.

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 ??

  1. 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.

  2. 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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.