Question
Plan and code a menu-driven well modularized (functions) program utilizing an array. Read all tabs. Modifying the previous lab. Store text entries and their length
Plan and code a menu-driven well modularized (functions) program utilizing an array. Read all tabs.
Modifying the previous lab. Store text entries and their length in an array of structures. Add the following menu options to
Print the text entries and their length in a table format with headings; text entries should be left aligned and numerical entries should be right aligned; tip: use setw()
Display stats
the longest word and its length
the shortest word and its length
the count of the text entries stored
the total number of characters in all text entries
Print text entries in ascending order (use bubble sort)
Quit
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Requirements/Specifications
One function = one job
Write one function to produce stats; go through the array of structs once only
Reading an Input File
Only go through the file once - do not read the file to count the number of entries, close it, and then read it again; someone can modify the file in the meantime
Output Formatting
output must be well-formatted, and easily understandable to those who have not seen the code
clearly label the output
Well document your code (comments); comments should be short and explain "why" and "what for"
Include test runs/ output as comment /* */ below all function definitions at the very bottom of the file with main()
this is my code I cannot go further
#include
const int kMaxSize = 100;
struct Entry { string text; int length; };
void DisplayMenu(); int ReadData(Entry data[], int maxSize, int& numDiscard); void PrintData(const Entry data[], int numEntries); void DisplayStats(const Entry data[], int numRead, int numStored, int numDiscard); void BubbleSortAsc(Entry data[], int numStored);
int main() { Entry data[kMaxSize]; int numEntries = 0; int numRead = 0; int numStored = 0; int numDiscard = 0; int choice;
do { DisplayMenu(); cout << "Enter your choice: "; cin >> choice; if (cin.fail() || choice < 1 || choice > 5) { cout << "Invalid choice. Please enter a number between 1 and 5." << endl; cin.clear(); cin.ignore(numeric_limits
switch (choice) { case 1: numRead = ReadData(data, kMaxSize, numDiscard); if (numRead == 0) { cout << "No entries found in inputfile.txt. " << endl; break; } numStored = numRead - numDiscard; PrintData(data, numStored); break;
case 2: DisplayStats(data, numRead, numStored, numDiscard); break;
case 3: cout << " The text entries in ascending order: " << endl; BubbleSortAsc(data, numStored); PrintData(data, numStored); break;
case 4: cout << " Program Ended. " << endl; cout << "See you next time! " << endl; break; } } while (choice != 4);
return 0; }
void DisplayMenu() { cout << " 1. Print values read from a file and stored in the array: " << endl; cout << "2. Display Stats: " << endl; cout << "3. Sort the text entries in ascending order: " << endl; cout << "4. Quit. " << endl; }
void DisplayStats(const Entry data[], int numRead, int numStored, int numDiscard) { cout << " Statistics: " << endl; cout << "Number of Entries Read: " << numRead << endl; cout << "Number of Entries Stored: " << numStored << endl; cout << "Number of Entries Discarded: " << numDiscard << endl; }
void BubbleSortAsc(Entry data[], int numStored) { bool swapped = false; do { swapped = false; for (int i = 0; i < numStored - 1; i++) { if (data[i].text > data[i + 1].text) { swap(data[i], data[i + 1]); swapped = true; } } } while (swapped); }
int ReadData(Entry data[], int maxSize, int& numDiscard) { string filename; ifstream inputFile; cout << "Enter the name of the file to read from: "; cin >> filename;
inputFile.open(filename); if (!inputFile.is_open()) { cout << "Unable
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started