filefunct.cpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. //filefunc.cpp -- function with ostream & parameter
  2. #include <iostream>
  3. #include <fstream>
  4. #include <cstdlib>
  5. using namespace std;
  6. void file_it(ostream & os, double fo, const double fe[],int n);
  7. const int LIMIT = 5;
  8. int main()
  9. {
  10. ofstream fout;
  11. const char * fn = "ep-data.txt";
  12. fout.open(fn);
  13. if (!fout.is_open())
  14. {
  15. cout << "Can't open " << fn << ". Bye.\n";
  16. exit(EXIT_FAILURE);
  17. }
  18. double objective;
  19. cout << "Enter the focal length of your "
  20. "telescope objective in mm: ";
  21. cin >> objective;
  22. double eps[LIMIT];
  23. cout << "Enter the focal lengths, in mm, of " << LIMIT
  24. << " eyepieces:\n";
  25. for (int i = 0; i < LIMIT; i++)
  26. {
  27. cout << "Eyepiece #" << i + 1 << ": ";
  28. cin >> eps[i];
  29. }
  30. file_it(fout, objective, eps, LIMIT);
  31. file_it(cout, objective, eps, LIMIT);
  32. cout << "Done\n";
  33. // cin.get();
  34. // cin.get();
  35. return 0;
  36. }
  37. void file_it(ostream & os, double fo, const double fe[],int n)
  38. {
  39. // save initial formatting state
  40. ios_base::fmtflags initial;
  41. initial = os.setf(ios_base::fixed, ios_base::floatfield);
  42. std::streamsize sz = os.precision(0);
  43. os << "Focal length of objective: " << fo << " mm\n";
  44. os.precision(1);
  45. os.width(12);
  46. os << "f.l. eyepiece";
  47. os.width(15);
  48. os << "magnification" << endl;
  49. for (int i = 0; i < n; i++)
  50. {
  51. os.width(12);
  52. os << fe[i];
  53. os.width(15);
  54. os << int (fo/fe[i] + 0.5) << endl;
  55. }
  56. // restore initial formatting state
  57. os.setf(initial, ios_base::floatfield);
  58. os.precision(sz);
  59. }