Question: What is this program doing? please give me a detailed description Code review assessment #include #include #include #include // file stream #include // cin, cout
What is this program doing? please give me a detailed description
Code review assessment
#include
#include
#include
#include
#include
#include
#include
using namespace std;
char menu(); // show user chocies
void execute(const char command); // perform a user's choice
void create_data();
void load_data(char const *fn); // load data from fn
void display_data();
void save_data(char const *fn); // save data to file fn
void about();
// menu choices
const char ABOUT = '0'; // use
characters it is safer for input
const char CREATE_DATA = '1';
const char LOAD_DATA = '2';
const char DISPLAY_DATA = '3';
const char SHOW_DIRECTORY = '4';
const char SAVE_DATA = '5';
const char SORT_DATA = '6';
const char QUIT = 'Q';
const int N = 100;
int myData[N]; // can hold 10 data items
char const *filename = "myData.txt"; // default data file name
int main()
{
char command;
do {
command = menu();
execute(command);
system("pause");
} while (toupper(command) != QUIT);
return 0;
}
char menu(){
char c;
do {
system("cls");
cout << "command menu ";
cout << "0.....About ";
cout << "1.....Create Data ";
cout << "2.....Load Data ";
cout << "3.....Display ";
cout << "4.....Directory ";
cout << "5.....Save Data ";
cout << "6.....Sort Data ";
cout << "Q.....Quit ";
cout << "choice: ";
cin >> c;
} while ( !('0'<=c && c<='6') && c!='Q');
return c;
}
void execute(const char command) {
switch (toupper(command)) {
case ABOUT:
about();
break;
case CREATE_DATA:
create_data();
break;
case LOAD_DATA:
load_data(filename);
break;
case SAVE_DATA:
save_data(filename);
break;
case DISPLAY_DATA:
display_data();
break;
case SHOW_DIRECTORY:
system("dir /a/p *.txt "); // only show files ending with
txt
break;
case SORT_DATA:
sort(myData, myData + N); // STL using default ordering
break;
} // end of switch statement
} // end of execute function
void about()
{
cout << " SAMPLE of menu driven programming ";
cout << "(c) copyright 2011 your name. All rights reserved. ";
}
void create_data()
{
cout << "Generating data ";
srand( time(NULL) ); // seed pseudo-random number generator
for (int i = 0; i myData[i] = rand() % 100; // pseudo-random value from 0 to 99. } void display_data() { cout << "Display data "; for (int i = 0; i < N; i++) { cout << myData[i] << '\t'; if ( (i+1) % 5 == 0) cout << endl; // skip a line only after 5 numbers are displayed //cout << myData[i] << endl; } } void save_data(char const *filename) { cout << "saving Data "; ofstream outfile; outfile.open(filename, ios_base::out); if (!outfile.is_open()) { cout << "file could not be created "; return; // system("PAUSE"); }; cout << "Begin Processing File" << endl; for (int i = 0; i { outfile << myData[i] << ' '; } outfile.close(); cout << "END Processing File" << endl; } // end of save the data function void load_data(char const *filename) { cout << "loading Data "; ifstream infile; // char const *filename = "myData.txt"; infile.open(filename, ios_base::in); if (!infile.is_open()) { cout << "file could not be created "; return; // system("PAUSE"); }; cout << "Begin Processing File" << endl; for (int i = 0; i { infile >> myData[i]; } infile.close(); cout << "END Processing File" << endl;
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
