Hello,
This is my first post. I am trying to teach myself C++ and am having a hard time with a function problem. This is not for homework, I simply want to learn something about C++ and I have no one else to ask.
The problem I am having is a simple function.
The problem in the book states:
Write a program that repeatedly asks the user to enter pairs of numbers until at least one of the pair is 0. For each pair, the program should use a function to calculate the harmonic mean of the numbers. The function should return the answer to main(), which should report the result. The harmonic mean of the numbers is the inverse of the average of the inverses and can be calculated as follows:
harmonic mean=2.0xXxY/(x+y)
Here is what I have gotten so far:
#include<iostream>
using namespace std;
void harmonic(int x, int y, float h);
int main()
{
int x;
int y;
float h;
while ( x != 0)
{
cout <<"Enter first integer: (or 0 to quit)";
cin >> x;
cout <<"Enter second integer: (or 0 to quit)";
cin >> y;
harmonic(x,y,h);
cout <<"The harmonic mean is: \n" << h <<endl;
}
system("PAUSE");
return EXIT_SUCCESS;
}
void harmonic(int x, int y, float h)
{
h=(2*x*y)/x+y;
}
I think as a side note I should mention that I am using DEV-C++.
The output, when I enter x=2 and y=2, is 3.21412e-039. When I enter a "0", the program simply stops.
What have I done wrong?
Any help will be greatly appreciated.