I was wondering how to write a sequential access text file using fixed point notation to 2 decimal places. What I tried in my code did not work, when I inputted 20 it appeared as 20 in the text file, when it should be 20.00.
//Ch13Ex16.cpp – saves the prices of inventory items
//in a sequential access file
#include <iostream>
#include <iomanip>
#include <fstream>
using std::cout;
using std::cin;
using std::endl;
using std::setprecision;
using std::setiosflags;
using std::ofstream;
using std::ios;
int main()
{
//declare variable
double price = 0.0;
//create file object and open the file
ofstream outFile;
outFile.open("prices.txt", ios::app);
//determine whether the file was opened
if (outFile.is_open())
{
//write output in fixed-point notation
//with two decimal places
//get the prices and
//write each to the file
cout << "Enter item price (-1 to stop): ";
cin >> setiosflags(ios::fixed) >> setprecision(2) >> price;
while (price != -1)
{
cin.ignore();
outFile << price << endl;
cout << "Enter item price (-1 to stop): ";
cin >> setiosflags(ios::fixed) >> setprecision(2) >> price;
} //end while
//close the file
outFile.close();
} //end if
else
cout << "File could not be opened." << endl;
return 0;
} //end of main function