Need help on my code?

Dell Inspiron 546 mini-tower desktop - c...
April 12, 2011 at 09:25:46
Specs: Windows 7, 1.5GB
Having a wrong output, can someone tell me where I go wrong and correct me.


#include <iostream>
using namespace std;


double calccelsius (double choice_par1);
double calcfahrenheit (double choice_par2);


int main ()

{
double Fahrenheit, Celsius, input;
int choice;

Celsius = calccelsius (choice);
Fahrenheit = calcfahrenheit (choice);

cout << "Choose 1 to convert Fahrenheit to Celsius" <<endl;
cout << "Choose 2 to convert Celsius to Fahrenheit" <<endl;
cout << "Please enter the number of choice: ";
cin >> choice;

if (choice == 1)
{
cout << "Please enter the value in Fahrenheit: ";
cin >> input;

cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
cout << "The equivalent of " << input << " Fahrenheit to Celsius is " << Celsius << " Celsius" <<endl;
}
else if (choice == 2)
{
cout << "Please enter the value in Celsius: ";
cin >> input;

cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
cout << "The equivalent of " << input << " Celsius to Fahrenheit is " << Fahrenheit << " Fahrenheit" <<endl;

}

system("pause");

return 0;
}

double calccelsius (double choice_par1)
{
double Celsius, input;

Celsius = static_cast<double>(5)/9 * (input - 32.0);

return Celsius;
}


double calcfahrenheit (double choice_par2)
{
double Fahrenheit, input;

Fahrenheit = static_cast<double>(9)/5 * input + 32.0;

return Fahrenheit;
}


See More: Need help on my code?

Report •


#1
April 12, 2011 at 11:50:43
You have to call the conversion functions after you have entered the value to be converted, not before. And when you do call them, you want "input" as the parameter, not "choice" (which is only ever "1" or "2")..

Report •

#2
April 12, 2011 at 23:36:09
Sorry I can't get what you mean.
Can you tweak my code and comment it.

Sorry I am still new to this language


Report •

#3
April 13, 2011 at 00:45:35
No, I'm afraid that I'm not going to tweak and comment the code for you. Just read through your code and look at what it is doing:

1. You declare some variables (Farenheit, Celcius, input, choice) which will have arbitrary values until you initialize them.

2. You call the two conversion functions, both times converting the (random) value of choice to the same (equally random) value.

3. You then input a value of "1" or "2" into choice.

4. You then input a value into input.

5. You then print out the value of either Celsius or Farenheit, both of which are uninitialized and so have totally random values.

Now think about what you actually want the program to do. If necessary, work through the program with pencil and paper, writing down the values of the various variables at each step (remember that, until you assign a value to them, they will contain random data).


Report •

Related Solutions

#4
April 13, 2011 at 06:03:55
Okay thanks for the advice somehow got it work properly. ^^

Report •


Ask Question