error1.cpp 649 B

123456789101112131415161718192021222324252627282930
  1. //error1.cpp -- using the abort() function
  2. #include <iostream>
  3. #include <cstdlib>
  4. double hmean(double a, double b);
  5. int main()
  6. {
  7. double x, y, z;
  8. std::cout << "Enter two numbers: ";
  9. while (std::cin >> x >> y)
  10. {
  11. z = hmean(x,y);
  12. std::cout << "Harmonic mean of " << x << " and " << y
  13. << " is " << z << std::endl;
  14. std::cout << "Enter next set of numbers <q to quit>: ";
  15. }
  16. std::cout << "Bye!\n";
  17. return 0;
  18. }
  19. double hmean(double a, double b)
  20. {
  21. if (a == -b)
  22. {
  23. std::cout << "untenable arguments to hmean()\n";
  24. std::abort();
  25. }
  26. return 2.0 * a * b / (a + b);
  27. }