Question: function definition(s) above main excessive use of print/cout/cin/ file stream/stringstream and/or endl and/or < < >> poor id arr return 0 means success not fail

function definition(s) above main

excessive use of print/cout/cin/ file stream/stringstream and/or endl and/or << >>

poor id arr

return 0 means success not fail

const

while(1) /while (true) loop/goto

uses continue and/or break (Ok in switch) and/or go to in loop(s) and/or if(s) (GET RID OF)

logic: calls get data when a user choose menu option

1. What if the content of the input file changes? What will happen with stats?

erroneous input can be handled in the default case of the switch, no if is needed on line 86 (GET RID OF)

unnecessary abrupt program termination in case quit (GET RID OF)

test runs included do not match program output (FIX)

how many times a user need to read print and show? (GET RID OF EXTRA PRINT OR SHOW)

UI should leave coupe of blank lines before and after the menu; it looks like word Menu is in the input file

no need for the . at the end of menu options; it is just a clutter but no useful info to a user

text entries should be left- aligned, we read text from left to right; numeric entries should be right-aligned with the same number of decimal places so numbers can be compared visually (hundreds vs thousands, etc)

invalid stats: input file has 11 entries only, yet it says 100 entries were read

CAN YOU EDIT THOSE FOR THIS LAB

#include #include #include #include

using namespace std;

const int MAX_SIZE = 100;

void printMenu() { cout << "Menu" << endl; cout << endl; cout << "1: print the array." << endl; cout << "2: print the stats." << endl; cout << "3: exit the program." << endl; }

void printArray(string arr[], int size) { cout << "Answer" << endl; for (int i = 0; i < size; i++) { cout << arr[i] << endl; } }

void printStats(int numRead, int numStored, int numDiscarded) { cout << "Text entries read | " << numRead << endl; cout << "Text entries stored | " << numStored << endl; cout << "Text entries discarded | " << numDiscarded << endl; }

int getData(string arr[]) { ifstream inputFile; inputFile.open("input.txt");

if (!inputFile.is_open()) { cout << "Error opening input file" << endl; return 0; }

int count = 0; string temp;

while (inputFile >> temp && count < MAX_SIZE) { if (temp.length() > 3) { arr[count] = temp; count++; cout << temp << endl; } else { cout << "Text entry discarded: " << temp << endl; } }

if (inputFile.eof()) { cout << "End of input file reached" << endl; } else { cout << "Input file too big for array" << endl; }

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" << endl; 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" << endl; return 0;

default: cout << "Invalid choice, please try again" << endl; } }

return 0; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!