cpp file:  // Corporate Sales Data Output using namespace std; #include #include #include #include "HeaderCla

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
icon
Concept explainers
Question

Two of the menu options (#2 and #3) are unfinished, and you need to complete the code necessary to write to and read from a binary file.  You will need to read through the code to find the functions that are called by these options, and then supply the missing code. Please help me get menu 2 and 3 to display properly.

the .cpp and .h have the information:

.cpp file: 

// Corporate Sales Data Output
using namespace std;

#include <iostream>
#include <fstream>
#include <stdlib.h>
#include "HeaderClassActivity1.h"

int main() {
    Sales qtrSales[NUM_QTRS];
    while (true) {
        cout << "Select an option: " << endl;
        cout << "1. Input data" << endl;
        cout << "2. Write data to file" << endl;
        cout << "3. Read data from file" << endl;
        cout << "4. Display sales" << endl;
        cout << "99. End program" << endl;
        int menuItem;
        cin >> menuItem;

        if (menuItem == 1) inputData(qtrSales);
        if (menuItem == 2) {
            cout << "Enter filename: ";
            string filename;
            cin >> filename;
            writeToFile(qtrSales, NUM_QTRS, filename);
        }
        if (menuItem == 3) {
            cout << "Enter filename: ";
            string filename;
            cin >> filename;
            readFromFile(qtrSales, NUM_QTRS, filename);
        }
        if (menuItem == 4) displaySales(qtrSales, NUM_QTRS);
        if (menuItem == 99) return 0;
    }
}

void inputData(Sales qtrSales[]) {
    string divs[] = { "East", "West", "North", "South" };
    int idx = 0;
    for (string div : divs) {
        for (int i = 0; i < 4; i++) {
            cout << "Enter sales for " << div << " division, quarter " << i + 1 << ": ";
            //cin >> qtrSales[idx].sales;
            // We are faking data entry here for test purposes!
            qtrSales[idx].sales = (i + 1) * 16.4 * RAND_MAX / rand();
            cout << qtrSales[idx].sales << endl;
            strncpy_s(qtrSales[idx].name, div.c_str(), STR_LEN);
            qtrSales[idx].qtr = i + 1;
            idx++;
        }
    }
}

void writeToFile(Sales qtrSales[], int size, string filename) {
    ofstream outFile;
    outFile.open(filename, ios::binary);
    for (int i = 0; i < size; i++) {
        // Loop through the array of Sales structs, output each to outFile
    }
    outFile.close();
}

void readFromFile(Sales qtrSales[], int size, string filename) {
    ifstream inFile;
    inFile.open(filename, ios::binary);
    for (int i = 0; i < size; i++) {
        // Read in each Sales struct from inFile to the Sales struct array
    }
    inFile.close();
}

 

HeaderClassActivity1.h file:

#pragma once

const int NUM_QTRS = 16;
const int STR_LEN = 6;

struct Sales {
    char name[STR_LEN];
    int qtr;
    double sales;
};

void inputData(Sales qtrSales[]);
void writeToFile(Sales qtrSales[], int size, string filename);
void readFromFile(Sales qtrSales[], int size, string filename);
void displaySales(Sales qtrSales[], int size);

void displaySales(Sales qtrSales[], int size) {
    for (int i = 0; i < size; i++) {
        cout << "Division: " << qtrSales[i].name << " quarter " << qtrSales[i].qtr << " sales: " << qtrSales[i].sales << endl;
    }
}

Expert Solution
Step 1

Given

Two of the menu options (#2 and #3) are unfinished, and you need to complete the code necessary to write to and read from a binary file.  You will need to read through the code to find the functions that are called by these options, and then supply the missing code. Please help me get menu 2 and 3 to display properly.

trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Control Structure
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education