In: Computer Science
The purpose of the program is for the user to be able to manage a grocery list. The user should be presented with a menu to either add and item or quit. The menu input should be case insensitive (e.g. 'a' and 'A' should both work to add).
At the top of the starting code, you will notice the function prototypes for two functions:
char chooseMenu()
this function should present the menu and return what the user inputs (as a character). However, if the user enters anything other than the options (a/A/q/Q), it should present the menu again.
vector addItem(vector )
this function should receive a vector of strings and ask the user to enter an item and get their input (spaces allowed). That item should be added to the end of the vector and then returned.
I have the following code done.
#include
#include
using namespace std;
// function prototypes
char chooseMenu();
vector addItem(vector );
// main program
int main() {
vector list;
char choice;
cout << "Welcome to Grocery List Manager\n";
cout << "===============================\n";
do{
choice = chooseMenu();
if( choice == 'a' || choice == 'A' ){
list = addItem(list);
}
}while( choice != 'q' && choice != 'Q' );
return 0;
}
// function definitions
// Menu input/output
cout << "Menu\n----\n";
cout << "(A)dd item\n";
cout << "(Q)uit\n";
cin >> input;
cin.ignore();
// item input/output
cout << "Enter item:\n";
getline(cin,input);
The program should take the following input. If someone enters a letter not a or q then it asks again if a or A is entered let the user enter an item to add if they enter q or Q stop program.
x
a
onions
a
cheese
A
tilapia
Q
#include <iostream>
#include <vector>
using namespace std;
// function prototypes
char chooseMenu();
vector <string> addItem(vector <string>);
// main program
int main() {
vector <string> list;
char choice;
cout << "Welcome to Grocery List Manager\n";
cout << "===============================\n";
do{
choice = chooseMenu();
if( choice == 'a' || choice == 'A' ){
list = addItem(list);
}
}while( choice != 'q' && choice != 'Q' );
return 0;
}
// function definitions
char chooseMenu(){
// Menu input/output
cout << "Menu\n----\n";
cout << "(A)dd item\n";
cout << "(Q)uit\n";
char input;
cin >> input;
cin.ignore();
return input;
}
vector <string> addItem(vector <string>A){
// item input/output
cout << "Enter item:\n";
string input;
getline(cin,input);
A.push_back(input);
return A;
}
Refer below for indentation and output preview.