Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ help please! /* This program will output sales data to an output file opened

ID: 3770704 • 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


/* 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;
string line;
// Write the statement to open a file called SalesData.txt in append mode
       outFile.open("SalesData.txt",std::ofstream::app);
// 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
outFile.close();
// Write code to create an input file object and a statement to open the file for input
ifstream inputFile;
inputFile.open("SalesData.txt",std::ifstream::in);
// Test to see if the file is open and if so read the prices and output them to the monitor
if(inputFile.is_open())
{  
   while ( getline(inputFile,line))
               {
                   cout << line << ' ';
               }
               }
               // Write code to close the file
       inputFile.close();
       }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote