Question

In: Computer Science

I need to only cout "grocery list" if the user actually entered items. If they didnt...

I need to only cout "grocery list" if the user actually entered items. If they didnt enter any items it should just cout "No need for groceries!". Everything else in the program is fine.

#include <iostream>
#include <vector>
using namespace std;

// function prototypes
char chooseMenu();
vector <string> addItem(vector <string>);
void showGroceries(vector <string> list);

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

void showGroceries(vector <string> list)
{
cout<<"Grocery list"<<endl;
for(int i=0; i<list.size();i++)
{


cout<<i+1<<"."<<list[i]<<endl;
}
while (list.empty())
{
cout<<"No need for groceries!";
break;
}

}

Solutions

Expert Solution

The change required in the code is:

In the function show groceries, First verify if the list is empty or not,if the list is empty then print no need for groceries message. Then place a If statement with the condition list not empty and then in the if block place the code to print the list items.

Changed Code:

#include <iostream>
#include <vector>
using namespace std;

// function prototypes
char chooseMenu();
vector <string> addItem(vector <string>);
void showGroceries(vector <string> list);

// 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;
}
////////////////////////////////////
//I made changes in this function
void showGroceries(vector <string> list)
{
//check if the list is empty
//if true then print the message
//No need for groceries
while(list.empty())
{
cout<<"No need for groceries!";
break;
}
//now check that if the list is not empty
//if the list is not empty all the list items will be printed
if(!list.empty()){

cout<<"Grocery list"<<endl;
for(int i=0; i<list.size();i++)
{


cout<<i+1<<"."<<list[i]<<endl;
}

}

}

Output:

Code Screenshot:

Code Snippet:

#include <iostream>
#include <vector>
using namespace std;

// function prototypes
char chooseMenu();
vector <string> addItem(vector <string>);
void showGroceries(vector <string> list);

// 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;
}
////////////////////////////////////
//I made changes in this function
void showGroceries(vector <string> list)
{
//check if the list is empty
//if true then print the message
//No need for groceries
while(list.empty())
{
cout<<"No need for groceries!";
break;
}
//now check that if the list is not empty
//if the list is not empty all the list items will be printed
if(!list.empty()){

cout<<"Grocery list"<<endl;
for(int i=0; i<list.size();i++)
{


cout<<i+1<<"."<<list[i]<<endl;
}

}

}

Related Solutions

Grocery List Program Write a program that keeps track of the user's grocery list items. Prompt...
Grocery List Program Write a program that keeps track of the user's grocery list items. Prompt the user if they'd like to (each action is a function): See the list Display all items (if any) in the list Add item to their list Confirm with the user before adding item (y/n or yes/no) If they enter a duplicate item, notify them the item already exists Remove items from their list Confirm with the user before removing item (y/n or yes/no)...
I need to implement a method that removes any duplicate items in a list and returns...
I need to implement a method that removes any duplicate items in a list and returns the new values    private static linkedlist removeDuplicates(linkedlist list) {    return list; }
JAVA You will be given a grocery list, filed by a sequence of items that have already been purchased.
 Instructions You will be given a grocery list, filed by a sequence of items that have already been purchased. You are going to determine which items remain on the the list and output them so that you know what to buy. You will be give an integer n that describes how many items are on the original grocery list. Following that, you will be given an array of n grocery list items (strings) that you need to buy. After your grocery list...
I need to draw a cylinder in java with user input, and I can't seem to...
I need to draw a cylinder in java with user input, and I can't seem to get my lines to line up with my ovals correctly from the users input... I know I will have to either add or subtract part of the radius or height but I'm just not getting it right, here is how I'm looking to do it.            g.drawOval(80, 110, radius, height);            g.drawLine(?, ?, ?, ?); g.drawLine(?, ?, ?, ?);   ...
I have 3 tables, but I need that when the user logs in and enters his...
I have 3 tables, but I need that when the user logs in and enters his login and password, it shows me only the information of that client. But it is showing me the information of all the clients. Bank(mid,code,cid,sid,type,amount,mydate,note) from CPS300 This is the table where I want the information of a client to be seen Customers(id,name,login,password) from CPS200 Sources(id,name) from CPS200 --------------------------------------------------------------------------------------------------------------------------------- for now I have two queries, but I must correct the one for client authentication because...
How would I fix my coding to allow the user to see what they have entered...
How would I fix my coding to allow the user to see what they have entered after they submit it? Where would I plug it into my code to see it? <!Doctype html> <html> <head> <meta charset="UTF-8"> <title>Login and Registeration Form Design</title> <link rel="stylesheet" type="text/css" href="../signintrial.css"> <script> function loginHandler(){ // code for handing login event } function registerHandler() {    //checking phone number    if(phonenumber(document.getElementById('phone').value))    {    //when phone number is valid    document.getElementById('demo').innerHTML = document.getElementById('fname').value + " "...
I want the linked list to be inserted by the user and donot use getline to...
I want the linked list to be inserted by the user and donot use getline to insert it #include <bits/stdc++.h> using namespace std;    // A linked list Node struct Node {     int data;     struct Node* next; };    // Size of linked list int size = 0;    // function to create and return a Node Node* getNode(int data) {     // allocating space     Node* newNode = new Node();        // inserting the required data     newNode->data = data;     newNode->next =...
hi i need a code that will give me this output, For the multiply_list, the user...
hi i need a code that will give me this output, For the multiply_list, the user will be asked to input the length of the list, then to input each element of the list. For the repeat_tuple, the user is only asked to enter the repetition factor, but not the tuple. Your program should take the list created before and convert it to a tuple. output expected: (**user input**) ******Create your List ****** Enter length of your list: 3 ******...
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...
I need a java code Write a simple program to prompt the user for a number...
I need a java code Write a simple program to prompt the user for a number between 1 and 12 (inclusive) and print out the corresponding month. For example:   The 12th month is December. Use a java "switch" statement to convert from the number (1-12) to the month. Also use a "do while" loop and conditional checking to validate that the number entered is between 1 and 12 inclusive and if not, prompt the user again until getting the correct...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT