-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMenu.cpp
More file actions
38 lines (32 loc) · 1.14 KB
/
Menu.cpp
File metadata and controls
38 lines (32 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include "Menu.h"
#include <iostream>
#include <limits>
using namespace std;
/*
* readMenuChoice()
* ----------------
* Prompts the user for a menu option in the range [1, 4].
* Re-prompts if the input is invalid or outside range.
* This function is separated from main.cpp to keep the main logic cleaner
* and to allow easy expansion if additional menu options are added later.
*/
int readMenuChoice() {
int choice;
while (true) {
cout << "\n---Welcome to Corner Grocer!---\n"
<< "Please select an option from the following:\n"
<< "1. Search for a specific fruit or vegetable.\n"
<< "2. Print all produce frequencies.\n"
<< "3. Print a histogram of all produce.\n"
<< "4. Exit the program.\n"
<< "Choose an option (1-4): " << flush;
if (cin >> choice && choice >= 1 && choice <= 4) {
cin.ignore(numeric_limits<streamsize>::max(), '\n');
return choice;
}
cout << "Invalid choice. Please enter 1, 2, 3, or 4.\n";
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
}
// End of Menu.cpp