cpp file: // Corporate Sales Data Output using namespace std; #include #include #include #include "HeaderCla
Control structures
Control structures are block of statements that analyze the value of variables and determine the flow of execution based on those values. When a program is running, the CPU executes the code line by line. After sometime, the program reaches the point where it has to make a decision on whether it has to go to another part of the code or repeat execution of certain part of the code. These results affect the flow of the program's code and these are called control structures.
Switch Statement
The switch statement is a key feature that is used by the programmers a lot in the world of programming and coding, as well as in information technology in general. The switch statement is a selection control mechanism that allows the variable value to change the order of the individual statements in the software execution via search.
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;
}
}

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
This is a popular solution!
Step by step
Solved in 2 steps









