fork download
  1. // Attached: Lab_6 Program #1
  2. // ===========================================================
  3. // File: Lab6_Program1.cpp
  4. // ===========================================================
  5. // Programmer: Elaine Torrez
  6. // Class: CS 1A
  7. // ===========================================================
  8.  
  9. #include <iostream>
  10. #include <fstream>
  11. #include <iomanip>
  12.  
  13. using namespace std;
  14.  
  15.  
  16. // ==== main ===================================================
  17. // This program writes one number to a text file twice.
  18. // The first output writes the number normally.
  19. // The second output formats the number to two decimal places.
  20. //
  21. // Input:
  22. // No user input.
  23. //
  24. // Output:
  25. // The number is written to the file values.txt twice,
  26. // once unformatted and once formatted to two decimal places.
  27. // ===========================================================
  28.  
  29. int main()
  30. {
  31. fstream dataFile;
  32. double number = 17.816392;
  33.  
  34. // open the file for output
  35. dataFile.open("values.txt");
  36.  
  37. // format for fixed-point notation
  38. dataFile << fixed;
  39.  
  40. // write number without limiting decimal places
  41. dataFile << number << endl;
  42.  
  43. // format output to 2 decimal places
  44. dataFile << setprecision(2);
  45.  
  46. // write number again
  47. dataFile << number << endl;
  48.  
  49. cout << "Data has been written to file.\n";
  50.  
  51. // close the file
  52. dataFile.close();
  53.  
  54. return 0;
  55.  
  56. } // end of main()
  57.  
  58. // ===========================================================
Success #stdin #stdout 0.01s 5308KB
stdin
Standard input is empty
stdout
Data has been written to file.