C++ help please! /* This program will output sales data to an output file opened
ID: 3770703 • Letter: C
Question
C++ help please! /* This program will output sales data to an output file opened in append mode. You will write the program, then run it twice to verify that the append mode worked. */ #include <iostream> #include <fstream> #include <iomanip> using namespace std; int main() { ofstream outFile; // Write the statement to open a file called SalesData.txt in append mode // Test to see if the file is open and if so get prices to write to it if (outFile.is_open()) { outFile << setprecision(2) << fixed << showpoint; // Want the output to be 2-decimal places double price; cout << "Enter a price: "; cin >> price; // priming read while (price != -1) // -1 is a sentinel value { outFile << price << endl; // write the price to the output file cout << "Enter a price: "; cin >> price; } // Write code to close the file // Write code to create an input file object and a statement to open the file for input // Test to see if the file is open and if so read the prices and output them to the monitor // Write code to close the file } }
Explanation / Answer
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
int main()
{
ofstream outFile;
outFile.open("SalesData.txt");
// Write the statement to open a file called SalesData.txt in append mode
// Test to see if the file is open and if so get prices to write to it
if (outFile.is_open())
{
outFile << setprecision(2) << fixed << showpoint; // Want the output to be 2-decimal places
double price;
cout << "Enter a price: ";
cin >> price; // priming read
while (price != -1) // -1 is a sentinel value
{
outFile << price << endl; // write the price to the output file
cout << "Enter a price: ";
cin >> price;
}
outFile.close();
// Write code to close the file
return 0;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.