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. How do I get menu 2 and 3 to display properly?

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
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. How do I 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
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY