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

9. (Inventory) Create an ASCII file with the following data, or use the shipped.

ID: 3911188 • Letter: 9

Question

9. (Inventory) Create an ASCII file with the following data, or use the shipped.dat file available on this book's Web site. The headings aren't part of the file but indicate what the data represents. Shipped Date 04/12/11 04/12/11 04/12/11 04/12/11 04/12/11 Tracking Number D50625 D60752 D40295 D23745 D50892 Part Number 74444 75255 74477 74470 75155 First Name James Janet Bill Diane Helen Last Name Lehoff Lezar McHen Kaiser Richardson NapTime Company Rotech Rotech Rotech Rotech The format of each line in the file is identical, with fixed-length fields defined as follows: Field Position Field Length Ending Col. No. 8 Field Name Starting Col. No. Shipped Date Tracking Number 12 Part Number First Name Last Name Compan 8 6 31 39 51 26 35 48 64 4 6 Using this data file, write a C++ program that reads the file and produces a report listing the shipped date, part number, first name, last name, and company name

Explanation / Answer

ScreenShot

----------------------------------------------------------------

Program

//Header files for I/O , file operation , diaplay formatting and string manipulation
#include<iostream>
#include<fstream>
#include<iomanip>
#include<string>>
using namespace std;
int main()
{
   //variables for file read
   string ShippedDate, trackingNumber, partNumber, firstName, lastName, company;
   //File read object
   ifstream in;
   //Open file
   in.open("C:/Users/deept/Desktop/Shipped.dat");
   //If not found
   if (!in) {
       cout<<"File not found ."<<endl;
       exit(0);
   }
   //Otherwise
   else {
       cout<< "ShippedDate    PartNumber    FirstName    LastName      Company -------------------------------------------------------------"<<endl;
       //Read until end of the line and display formatted output
       while (!in.eof()) {
           in >> ShippedDate>>trackingNumber>>partNumber>>firstName>>lastName>>company;
           std::cout << std::setw(10) << std::setfill(' ');
           cout << ShippedDate;
           std::cout << std::setw(15) << std::setfill(' ');
           cout<< partNumber;
           std::cout << std::setw(9) << std::setfill(' ');
           cout<< firstName << "   ";
           std::cout<< std::setw(12)<< std::setfill(' ');
           cout << lastName;
           std::cout << std::setw(14)<<std::setfill(' ');
           cout<< company << endl;
       }
   }
    return 0;
}

----------------------------------------------------------------

Output

ShippedDate    PartNumber    FirstName    LastName      Company
------------------------------------------------------------------------------
04/12/11          74444           James         Lehoff        Rotech
04/12/11          75255           Janet          Lezar          Rotech
04/12/11          74477           Bill           McHenry        Rotech
04/12/11          74470         Diane         Kaiser           Rotech
04/12/11          75155        Helen     Richardson       NapTime
Press any key to continue . . .