1
0

outfile.cpp 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // outfile.cpp -- writing to a file
  2. #include <iostream>
  3. #include <fstream> // for file I/O
  4. int main()
  5. {
  6. using namespace std;
  7. char automobile[50];
  8. int year;
  9. double a_price;
  10. double d_price;
  11. ofstream outFile; // create object for output
  12. outFile.open("carinfo.txt"); // associate with a file
  13. cout << "Enter the make and model of automobile: ";
  14. cin.getline(automobile, 50);
  15. cout << "Enter the model year: ";
  16. cin >> year;
  17. cout << "Enter the original asking price: ";
  18. cin >> a_price;
  19. d_price = 0.913 * a_price;
  20. // display information on screen with cout
  21. cout << fixed;
  22. cout.precision(2);
  23. cout.setf(ios_base::showpoint);
  24. cout << "Make and model: " << automobile << endl;
  25. cout << "Year: " << year << endl;
  26. cout << "Was asking $" << a_price << endl;
  27. cout << "Now asking $" << d_price << endl;
  28. // now do exact same things using outFile instead of cout
  29. outFile << fixed;
  30. outFile.precision(2);
  31. outFile.setf(ios_base::showpoint);
  32. outFile << "Make and model: " << automobile << endl;
  33. outFile << "Year: " << year << endl;
  34. outFile << "Was asking $" << a_price << endl;
  35. outFile << "Now asking $" << d_price << endl;
  36. outFile.close(); // done with file
  37. // cin.get();
  38. // cin.get();
  39. return 0;
  40. }