Question
The code provided has several areas that could be improved, such as using a while loop instead of a while(1) or while(true) loop, avoiding the
The code provided has several areas that could be improved, such as using a while loop instead of a while(1) or while(true) loop, avoiding the excessive use of print/cout/cin/ file stream/stringstream and/or endl and/or << >>, improving the variable names, returning 0 for success instead of failure, and avoiding the use of continue, break, and goto in loops and if statements. Additionally, the code could benefit from handling erroneous input in the default case of the switch statement, avoiding unnecessary abrupt program termination, and ensuring that the test runs match the program output. Lastly, it is recommended to leave a couple of blank lines before and after the menu, left-align text entries, right-align numeric entries with the same number of decimal places, and fix invalid stats where the input file has fewer entries than the program claims to have read.
CODING
#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
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;
}
Inside of input.txt
Spam Atmosphere Gym Space Science Movies Free Spam Entertainment Answer 5678
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