Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

CODE TO EDIT #include #include #include #include using namespace std; const int MAX_SIZE = 100; void printMenu() { cout < < Menu ; cout <

CODE TO EDIT

#include

#include

#include

#include

using namespace std;

const int MAX_SIZE = 100;

void printMenu() {

cout << "Menu ";

cout << "1: Print the array ";

cout << "2: Print the stats ";

cout << "3: Exit the program ";

}

void printArray(string arr[], int size) {

cout << "Array: ";

for (int i = 0; i < size; i++) {

cout << arr[i] << endl;

}

}

void printStats(int numRead, int numStored, int numDiscarded) {

cout << "Stats: ";

cout << "Text entries read: " << numRead << endl;

cout << "Text entries stored: " << numStored << endl;

cout << "Text entries discarded: " << numDiscarded << endl;

}

int getData(string arr[]) {

ifstream inputFile("input.txt");

if (!inputFile.is_open()) {

cout << "Error opening input file ";

return 0;

}

int count = 0;

string temp;

while (inputFile >> temp && count < MAX_SIZE) {

if (temp.length() > 3) {

arr[count] = temp;

count++;

}

else {

cout << "Text entry discarded: " << temp << endl;

}

}

if (inputFile.eof()) {

cout << "End of input file reached ";

}

else {

cout << "Input file too big for array ";

}

inputFile.close();

return count;

}

int main() {

string arr[MAX_SIZE];

int size = 0;

int numRead = 0, numStored = 0, numDiscarded = 0;

while (true) {

printMenu();

int choice;

cin >> choice;

if (cin.fail()) {

cin.clear();

cin.ignore(numeric_limits::max(), ' ');

cout << "Invalid input, please enter a number ";

continue;

}

switch (choice) {

case 1:

size = getData(arr);

numRead = MAX_SIZE;

numStored = size;

numDiscarded = MAX_SIZE - size;

printArray(arr, size);

break;

case 2:

printStats(numRead, numStored, numDiscarded);

break;

case 3:

cout << "Exiting program ";

return 0;

default:

cout << "Invalid choice, please try again ";

break;

}

}

return 0;

}

EDITS TO MAKE

use setw() to format output

do not mix setw() and '\t'; it could produce different results on different computers

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 along with their length (use bubble sort)

Quit

Things that should be in the input file:

madison

apples

grapes

fruit

rocks

socks

gym

purple

finance

underwire

sprint

spray

sugar

snap

IT IS JUST A LIST OF RANDOM WORDS

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Concepts

Authors: David Kroenke

4th Edition

0136086535, 9780136086536

More Books

Students also viewed these Databases questions

Question

What does stickiest refer to in regard to social media

Answered: 1 week ago