Menu

M Coding C M Attempting Alter Current Code Align Order Txt File Currently Order Txt File Q43887755

So I’m coding in C++ and I’m attempting to alter my current codebelow to align with the order my .txt file is currently in. Here isthe order of my .txt file:

Manufacturer

Processor

RAM

Disk Size

Cost

Location

Date Acquired

Asset Tag

I am attempting to re-arrange it to output the followingorder:

Asset Tag: 20191231-PC-021 03/24/2007 Acquired: $1087.99 Cost: 256 Location: Manufacturer: Dell Core i7 8700 (3.2 GHz) CPU: R

My current C++ code:

#include<iostream>
#include<string>
#include<fstream>
#include<iomanip>
using namespace std;

struct Date{
short day;
short month;
int year;
};

struct Inventory{
  
   string manufacturer;
   string processor;
   int ram;
   int disk_size;
   double cost;
   string location;
   Date date_acquired;
   string asset_tag;
};

Inventory read_record(ifstream &inFile);

void format_record(const Inventory &inv);

int main(){
   ifstream inFile;
   inFile.open(“input.txt”);
   if(inFile.fail()){
       cout << “Unable to open file”<< endl;
  
       return -1;
   }

   Inventory record;

   while(!inFile.eof()){
       record = read_record(inFile);
       format_record(record);
   }

   inFile.close();

   return 0;
}

Inventory read_record(ifstream &inFile){
   Inventory rec;
   inFile >> rec.asset_tag >>rec.date_acquired.month >> rec.date_acquired.day >>rec.date_acquired.year >> rec.cost;
   getline(inFile, rec.location);
   getline(inFile, rec.location);
   getline(inFile, rec.manufacturer);
   getline(inFile, rec.processor);
   inFile >> rec.ram >> rec.disk_size;
   return rec;
}

void format_record(const Inventory &inv){
   cout <<“——————————————” << endl;
   cout << setprecision(2) << fixed;
   cout << setw(16) << left << “AssetTag:” << inv.asset_tag << endl;
   cout << setw(16) << “Acquired:” <<right << setw(2) << setfill(‘0’) <<inv.date_acquired.month << “/” << setw(2) <<setfill(‘0’) << inv.date_acquired.day << “/” <<inv.date_acquired.year << endl;
   cout << setfill(‘ ‘) << left;
   cout << setw(16) << “Cost:”<<“$”<< inv.cost << endl;
   cout << setw(16) << “Location:” <<inv.location << endl;
   cout << setw(16) << “Manufacturer:”<< inv.manufacturer << endl;
   cout << setw(16) << “CPU:” <<inv.processor << endl;
   cout << setw(16) << “RAM:” <<inv.ram << ” GB” << endl;
   cout << setw(16) <<“Disk:”<<inv.disk_size<<” GB” << endl;
   cout <<“——————————————” << endl;
}

Asset Tag: 20191231-PC-021 03/24/2007 Acquired: $1087.99 Cost: 256 Location: Manufacturer: Dell Core i7 8700 (3.2 GHz) CPU: RAM: 16 GB Disk: 256 GB Asset Tag: 20191215-PC-045 08/01/2010 Acquired: $459.00 Cost: Location: 128 НР Сompaq Ryzen 2400GE (3.20 GHz) 8 GB Manufacturer: CPU: RAM: Disk: 1024 GB Show transcribed image text Asset Tag: 20191231-PC-021 03/24/2007 Acquired: $1087.99 Cost: 256 Location: Manufacturer: Dell Core i7 8700 (3.2 GHz) CPU: RAM: 16 GB Disk: 256 GB Asset Tag: 20191215-PC-045 08/01/2010 Acquired: $459.00 Cost: Location: 128 НР Сompaq Ryzen 2400GE (3.20 GHz) 8 GB Manufacturer: CPU: RAM: Disk: 1024 GB

Expert Answer


Answer to So I’m coding in C++ and I’m attempting to alter my current code below to align with the order my .txt file is currently…

OR