Question

In: Computer Science

#include <iostream> #include <string> #include <iomanip> #include <fstream> using namespace std; struct Product {    string...

#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>

using namespace std;
struct Product
{
   string itemname;
   int id;
   string itemcolor;
   double cost;
};

void fillProduct(Product[10], int& );//read data from a file and store in an array
void writeProduct(Product[10], int);//write the array into a file
void writeBinary(Product[10], int);//write the array into a file in binary mode
void printbinary(Product[10], int);//read data from the binary file and print


int main()
{
   Product myList[10];
   int numItems = 0;
   fillProduct(myList, numItems);
   writeProduct(myList, numItems);
   writeBinary(myList, numItems);
   printbinary(myList, numItems);
   return 0;
}


void fillProduct(Product myList[10], int& numItems)
{
   ifstream indata;
   indata.open("lab6data.txt");
   if (!indata)
   {
       cout << "Error opening file. Program aborting.\n";
   }
   else
   {
       string tname;
       getline(indata, tname);
       while (!indata.eof())
       {
           myList[numItems].itemname = tname;
           indata >> myList[numItems].id;
           indata.ignore();
           getline(indata, myList[numItems].itemcolor);
           indata >> myList[numItems].cost;
           indata.ignore();
           numItems++;
           getline(indata, tname);
          
       } // END WHILE
   }
   indata.close();
}

void writeProduct(Product myList[10], int numItems)
{
   ofstream outdata;
   outdata.open("output.txt");
   if (!outdata)
   {
       cout << "Error opening file. Program aborting.\n";
   }
   else
   {
       outdata << left;
       outdata << setw(15) << "Item Name"
           << setw(15) << "ID"
           << setw(15) << "Color"
           << setw(15) << "Cost" << endl;
       outdata << setw(15) << "---------"
           << setw(15) << "--"
           << setw(15) << "-----"
           << setw(15) << "----" << endl;
       for (int k = 0; k < numItems; k++)
       {
           outdata << setw(15) << myList[k].itemname
                   << setw(15) << myList[k].id
                   << setw(15) << myList[k].itemcolor
                   << setw(15) << myList[k].cost << endl;
       }
   }
   outdata.close();
}

void writeBinary(Product myList[10], int numItems)
{
   ofstream outbinary;
   outbinary.open("binary.dat", ios::binary);
   if (!outbinary)
   {
       cout << "Error opening file. Program aborting.\n";
   }
   else
   {
       for (int k = 0; k < numItems; k++)
       {
           //TODO Write a single statement below that writes data in myList[k] into binary.dat
      
       }

   }
   outbinary.close();
}


void printbinary(Product myList[10],int numItems) {
   ifstream inbinary;
   inbinary.open("binary.dat", ios::binary);
   if (!inbinary)
   {
       cout << "Error opening file. Program aborting.\n";
   }
   else
   {
       cout << left;
       cout<< setw(15) << "Item Name"
           << setw(15) << "ID"
           << setw(15) << "Color"
           << setw(15) << "Cost" << endl;
       cout << setw(15) << "---------"
           << setw(15) << "--"
           << setw(15) << "-----"
           << setw(15) << "----" << endl;

       for (int k = 0; k < numItems; k++)
       {
           //TODO Write a single statement below that read data from binary.dat and store them in myList[k].
          

           //TODO Print the data stored in myList[k], and the output should have the same format as the example in the instruction.
          
       }
   }
   inbinary.close();

}

Solutions

Expert Solution

C++ code screenshot:

C++ code:

#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>

using namespace std;
struct Product
{
    string itemname;
    int id;
    string itemcolor;
    double cost;
};

void fillProduct(Product[10], int &); //read data from a file and store in an array
void writeProduct(Product[10], int);  //write the array into a file
void writeBinary(Product[10], int);   //write the array into a file in binary mode
void printbinary(Product[10], int);   //read data from the binary file and print

int main()
{
    Product myList[10];
    int numItems = 0;
    fillProduct(myList, numItems);
    writeProduct(myList, numItems);
    writeBinary(myList, numItems);
    printbinary(myList, numItems);
    return 0;
}

void fillProduct(Product myList[10], int &numItems)
{
    ifstream indata;
    indata.open("lab6data.txt");
    if (!indata)
    {
        cout << "Error opening file. Program aborting.\n";
    }
    else
    {
        string tname;
        getline(indata, tname);
        while (!indata.eof())
        {
            myList[numItems].itemname = tname;
            indata >> myList[numItems].id;
            indata.ignore();
            getline(indata, myList[numItems].itemcolor);
            indata >> myList[numItems].cost;
            indata.ignore();
            numItems++;
            getline(indata, tname);

        } // END WHILE
    }
    indata.close();
}

void writeProduct(Product myList[10], int numItems)
{
    ofstream outdata;
    outdata.open("output.txt");
    if (!outdata)
    {
        cout << "Error opening file. Program aborting.\n";
    }
    else
    {
        outdata << left;
        outdata << setw(15) << "Item Name"
                << setw(15) << "ID"
                << setw(15) << "Color"
                << setw(15) << "Cost" << endl;
        outdata << setw(15) << "---------"
                << setw(15) << "--"
                << setw(15) << "-----"
                << setw(15) << "----" << endl;
        for (int k = 0; k < numItems; k++)
        {
            outdata << setw(15) << myList[k].itemname
                    << setw(15) << myList[k].id
                    << setw(15) << myList[k].itemcolor
                    << setw(15) << myList[k].cost << endl;
        }
    }
    outdata.close();
}

void writeBinary(Product myList[10], int numItems)
{
    ofstream outbinary;
    outbinary.open("binary.dat", ios::binary);
    if (!outbinary)
    {
        cout << "Error opening file. Program aborting.\n";
    }
    else
    {
        for (int k = 0; k < numItems; k++)
        {
            //TODO Write a single statement below that writes data in myList[k] into binary.dat
            outbinary.write((char *)&myList[k], sizeof(Product));
        }
    }
    outbinary.close();
}

void printbinary(Product myList[10], int numItems)
{
    ifstream inbinary;
    inbinary.open("binary.dat", ios::binary);
    if (!inbinary)
    {
        cout << "Error opening file. Program aborting.\n";
    }
    else
    {
        cout << left;
        cout << setw(15) << "Item Name"
             << setw(15) << "ID"
             << setw(15) << "Color"
             << setw(15) << "Cost" << endl;
        cout << setw(15) << "---------"
             << setw(15) << "--"
             << setw(15) << "-----"
             << setw(15) << "----" << endl;

        for (int k = 0; k < numItems; k++)
        {
            //TODO Write a single statement below that read data from binary.dat and store them in myList[k].
            inbinary.read((char *)&myList[k], sizeof(Product));
            //TODO Print the data stored in myList[k], and the output should have the same format as the example in the instruction.
            cout << setw(15) << myList[k].itemname
                 << setw(15) << myList[k].id
                 << setw(15) << myList[k].itemcolor
                 << setw(15) << myList[k].cost << endl;
        }
    }
    inbinary.close();
}

Output:


Related Solutions

Complete the following program #include<iostream> #include<iomanip> #include<fstream> using namespace std; int main() { // I -...
Complete the following program #include<iostream> #include<iomanip> #include<fstream> using namespace std; int main() { // I - Declaring a five by five array /* II - Read data from data.txt and use them to create the matrix in the previous step*/    // III - Count and print the number of even integers in the matrix. /* IV - Calculate and print the sum of all integers in the columns with an even index value. Please note the column index begins...
#include <iostream> #include <string> #include <fstream> #include <vector> #include <sstream> using namespace std; int main() {...
#include <iostream> #include <string> #include <fstream> #include <vector> #include <sstream> using namespace std; int main() { ifstream infile("worldpop.txt"); vector<pair<string, int>> population_directory; string line; while(getline(infile, line)){ if(line.size()>0){ stringstream ss(line); string country; int population; ss>>country; ss>>population; population_directory.push_back(make_pair(country, population)); } } cout<<"Task 1"<<endl; cout<<"Names of countries with population>=1000,000,000"<<endl; for(int i=0;i<population_directory.size();i++){ if(population_directory[i].second>=1000000000){ cout<<population_directory[i].first<<endl; } } cout<<"Names of countries with population<=1000,000"<<endl; for(int i=0;i<population_directory.size();i++){ if(population_directory[i].second<=1000000){ cout<<population_directory[i].first<<endl; } } } can u pls explain the logic behind this code up to 10 lines pls, many thanks
#include <iostream> #include <string> #include <iomanip> #include <cstdlib> #include "Contact.h" using namespace std; class Contact {...
#include <iostream> #include <string> #include <iomanip> #include <cstdlib> #include "Contact.h" using namespace std; class Contact { public: Contact(string init_name = "", string init_phone = "000-000-0000"); void setName(string name); void setPhone(string phone); string getName()const; string getPhone()const; friend ostream& operator << (ostream& os, const Contact& c); friend bool operator == (const Contact& c1, const Contact& c2); friend bool operator != (const Contact& c1, const Contact& c2); private: string name, phone; }; Contact::Contact(string init_name, string init_phone) { name = init_name; phone = init_phone;...
#include <iostream> #include <fstream> #include <string> using namespace std; const int QUIZSIZE = 10; const int...
#include <iostream> #include <fstream> #include <string> using namespace std; const int QUIZSIZE = 10; const int LABSIZE = 10; const int PROJSIZE = 3; const int EXAMSIZE = 3; float getAverage(float arr[], int size) { float total = 0; for (int i = 0; i < size; i++) { total += arr[i]; } return total/size; } // the following main function do.... int main() { ifstream dataIn; string headingLine; string firstName, lastName; float quiz[QUIZSIZE]; float lab[LABSIZE]; float project[PROJSIZE]; float midExam[EXAMSIZE];...
Array based application #include <iostream> #include <string> #include <fstream> using namespace std; // Function prototypes void...
Array based application #include <iostream> #include <string> #include <fstream> using namespace std; // Function prototypes void selectionSort(string [], int); void displayArray(string [], int); void readNames(string[], int); int main() {    const int NUM_NAMES = 20;    string names[NUM_NAMES];    // Read the names from the file.    readNames(names, NUM_NAMES);    // Display the unsorted array.    cout << "Here are the unsorted names:\n";    cout << "--------------------------\n";    displayArray(names, NUM_NAMES);    // Sort the array.    selectionSort(names, NUM_NAMES);    //...
#include <iostream> #include <fstream> #include <vector> using namespace std; struct Point{ int x, y; bool operator==(const...
#include <iostream> #include <fstream> #include <vector> using namespace std; struct Point{ int x, y; bool operator==(const Point& p2) { return this->x == p2.x and this->y == p2.y; } bool operator!=(const Point& p2) { return this->x != p2.x or this->y != p2.y; } friend ostream &operator<<( ostream &out, const Point &P ) { out << "(" << P.x << ", " << P.y << ")"; return out; } friend istream &operator>>( istream &in, Point &P ) { char d1, d2, d3;...
What is the flowchart for this code. Thank You! #include<iostream> #include<iomanip> #include<string> #include<cmath> using namespace std;...
What is the flowchart for this code. Thank You! #include<iostream> #include<iomanip> #include<string> #include<cmath> using namespace std; float series(float r[], int n) {    float sum = 0;    int i;    for (i = 0; i < n; i++)        sum += r[i];    return sum; } float parallel(float r[], int n) {    float sum = 0;    int i;    for (i = 0; i < n; i++)        sum = sum + (1 / r[i]);...
#include <iostream> #include <iomanip> using namespace std; int main() {             float miles;   //miles traveled          &nbsp
#include <iostream> #include <iomanip> using namespace std; int main() {             float miles;   //miles traveled             float hours;   //time in hours             float milesPerHour; //calculated miles per hour             cout << "Please input the Miles traveled" << endl;             cin >> miles;             cout << "Please input the hours traveled" << endl;             cin >> hours;                         milesHours = miles / hours; cout << fixed << showpoint << setprecision(2);             cout << "Your speed is " <<...
#include <iostream> #include <iomanip> using namespace std; int main() {     int studentid, numberreverse[20], count =...
#include <iostream> #include <iomanip> using namespace std; int main() {     int studentid, numberreverse[20], count = 0, maximum = 0, minimum = 0;     cout << "Enter your student ID number: ";     cin >> studentid;     cout << "Student ID Number = " << studentid << endl;     while (studentid != 0)     {          numberreverse[count] = studentid % 10;          if (count == 0)          {              minimum = numberreverse[count];              maximum = minimum;          }          else...
--- TURN this Code into Java Language --- #include <iostream> #include <string> using namespace std; //...
--- TURN this Code into Java Language --- #include <iostream> #include <string> using namespace std; // constants const int FINAL_POSITION = 43; const int INITIAL_POSITION = -1; const int NUM_PLAYERS = 2; const string BLUE = "BLUE"; const string GREEN = "GREEN"; const string ORANGE = "ORANGE"; const string PURPLE = "PURPLE"; const string RED = "RED"; const string YELLOW = "YELLOW"; const string COLORS [] = {BLUE, GREEN, ORANGE, PURPLE, RED, YELLOW}; const int NUM_COLORS = 6; // names...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT