Question

In: Computer Science

I have the function call showGroceries(list); I need to create a new prototype above int main...

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;
}

Solutions

Expert Solution

#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.


Related Solutions

These are a list of the transactions I have from playing Monopoly. I need to create...
These are a list of the transactions I have from playing Monopoly. I need to create a balance sheet, income statement, and cash flow. STOCK- Issued $1500 common stock LAND/STOCK- $600 issued for purchase of Land 1 RENT REVENUE- $16.00 From New York Ave 2 CONSULTING REV(ACCTS REC.) - Collected $200 for passing Go 3 LAND - Purchase Kentucky Ave $220.00 4 RENT EXPENSE - $200.00 for landing on Railroad 5 CONSULTING REV(ACCTS REC.) - Collected $200 for passing Go...
#include <stdio.h> int sum(int n); //prototype int main() {     int number, result;     printf("Enter a positive integer:...
#include <stdio.h> int sum(int n); //prototype int main() {     int number, result;     printf("Enter a positive integer: ");     scanf("%d", &number);     result = sum(number);     printf("sum = %d", result);     return 0; } int sum(int n) {     if (n != 0)         return n + sum(n-1);     else         return n; } What does the code above do?
In python I have a linked list. I want to create one function that takes in...
In python I have a linked list. I want to create one function that takes in one parameter, head. In the function, cur = head and next_cur = head.next. I want to return head and next_cur, except at the end of the function they will return alternating values from head. For example, if the these are the values in the linked list: 2, 3, 5, 7, 11 after the function head should return: 2, 5, 11 and next_cur should return:...
Write a function with the following prototype: int add_ok (int s, unsigned x, unsigned y); This...
Write a function with the following prototype: int add_ok (int s, unsigned x, unsigned y); This function should return 1 if arguments x and y can be added without causing overflow, otherwise return 0. If s is 0, x and y are unsigned numbers. If s is 1, x and y are treated as signed numbers. I tried this code but it doesn't work. Does anyone help me please? #include int add_ok(int s, unsigned x, unsigned y) { s=x+y; if...
In C++ Prototype your functions above "main" and define them below "main"; Write a program that...
In C++ Prototype your functions above "main" and define them below "main"; Write a program that uses two identical arrays of at least 20 integers. It should call a function that uses the bubble sort algorithm to sort one of the arrays in ascending order. The function should keep count of the number of exchanges it makes. The program then should call a function that uses the selection sort algorithm to sort the other arrays. It should also keep count...
In C++ prototype functions above "main" and define them below "main"; Write a program that uses...
In C++ prototype functions above "main" and define them below "main"; Write a program that uses two identical arrays of at least 20 integers. It should call a function that uses the bubble sort algorithm to sort one of the arrays in ascending order. The function should keep count of the number of exchanges it makes. The program then should call a function that uses the selection sort algorithm to sort the other arrays. It should also keep count of...
USING PYTHON Write a program to create a number list. It will call a function to...
USING PYTHON Write a program to create a number list. It will call a function to calculate the average values in the list. Define main ():                        Declare variables and initialize them                        Create a list containing numbers (int/float)                        Call get_avg function that will return the calculated average values in the list.                                       Use a for loop to loop through the values in the list and calculate avg                        End main()
I need to create a linked list that contains a fixed arraylist. Each new entry is...
I need to create a linked list that contains a fixed arraylist. Each new entry is added to array list. If the arraylist is full, create a new arraylist and add to the linklist. In java please.
int main(){    int i = 3;    if(fork()){    i++;    fork();    i+=3;   ...
int main(){    int i = 3;    if(fork()){    i++;    fork();    i+=3;    }else{    i+=4;    fork();    i+=5;    }    printf("%d\n",i); } how many processes does this code create including the parent
int main(){    int i = 3;    if(fork()){    i++;    fork();    i+=3;   ...
int main(){    int i = 3;    if(fork()){    i++;    fork();    i+=3;    }else{    i+=4;    fork();    i+=5;    }    printf("%d\n",i); } what is the output of the code
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT