1
0

error2.cpp 850 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. //error2.cpp -- returning an error code
  2. #include <iostream>
  3. #include <cfloat> // (or float.h) for DBL_MAX
  4. bool hmean(double a, double b, double * ans);
  5. int main()
  6. {
  7. double x, y, z;
  8. std::cout << "Enter two numbers: ";
  9. while (std::cin >> x >> y)
  10. {
  11. if (hmean(x,y,&z))
  12. std::cout << "Harmonic mean of " << x << " and " << y
  13. << " is " << z << std::endl;
  14. else
  15. std::cout << "One value should not be the negative "
  16. << "of the other - try again.\n";
  17. std::cout << "Enter next set of numbers <q to quit>: ";
  18. }
  19. std::cout << "Bye!\n";
  20. return 0;
  21. }
  22. bool hmean(double a, double b, double * ans)
  23. {
  24. if (a == -b)
  25. {
  26. *ans = DBL_MAX;
  27. return false;
  28. }
  29. else
  30. {
  31. *ans = 2.0 * a * b / (a + b);
  32. return true;
  33. }
  34. }