In: Computer Science
I have the function call showGroceries(list); I need to create a new prototype above int main that provides:
return type
function name
parameter(s) type(s)
Then copy-and-paste the prototype below int main. Give the parameter a name and then implement the function so that it takes the vector of strings and displays it so that a vector with the values:
{"milk", "bread", "corn"}
would display as:
Grocery list
1. milk
2. bread
3. corn
However, if there is nothing in the vector, instead it should display:
No need for groceries!
I have the following code
#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' );
showGroceries(list);
return 0;
}
// function definitions
char chooseMenu()
{
char input;
// Menu input/output
cout << "Menu\n----\n";
cout << "(A)dd item\n";
cout << "(Q)uit\n";
cin >> input;
cin.ignore();
return input;
}
vector <string> addItem(vector <string>A)
{
string input;
// item input/output
cout << "Enter item:\n";
getline(cin,input);
A.push_back(input);
return A;
}
#include <iostream>
#include <vector>
using namespace std;
// function prototypes
char chooseMenu();
vector <string> addItem(vector <string>);
void showGroceries(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' );
showGroceries(list);
return 0;
}
// function definitions
char chooseMenu()
{
char input;
// Menu input/output
cout << "Menu\n----\n";
cout << "(A)dd item\n";
cout << "(Q)uit\n";
cin >> input;
cin.ignore();
return input;
}
vector <string> addItem(vector <string>A)
{
string input;
// item input/output
cout << "Enter item:\n";
getline(cin,input);
A.push_back(input);
return A;
}
// desired function definition
void showGroceries(vector <string> A)
{
if(A.size()==0)
cout<<"No need for groceries!";
for(int i=0; i<A.size();i++)
{
cout<<i+1<<". "<<A[i]<<endl;
}
}
The function definition is at the end. You can refer below for output preview.