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; }
Read in the names of several grocery items from the keyboard and create a shopping list...
Read in the names of several grocery items from the keyboard and create a shopping list using the Java ArrayList abstract data type. Flow of Program: 1) Create a new empty ArrayList 2) Ask the user for 5 items to add to a shopping list and add them to the ArrayList (get from user via the keyboard). 3) Prompt the user for an item to search for in the list. Output a message to the user letting them know whether...
JAVA: Compute the average of a list of user-entered integers representing rolls of two dice. The...
JAVA: Compute the average of a list of user-entered integers representing rolls of two dice. The list ends when 0 is entered. Integers must be in the range 2 to 12 (inclusive); integers outside the range don't contribute to the average. Output the average, and the number of valid and invalid integers (excluding the ending 0). If only 0 is entered, output 0. The output may be a floating-point value. Ex: If the user enters 8 12 13 0, the...
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...
C++... How do I separate a C String, entered by a user, by commas? Say they...
C++... How do I separate a C String, entered by a user, by commas? Say they enter Frog,Wolf,Spider... I want a array containing the elements {Frog, Wolf, Spider} thanks Here's my idea so far... cout << "Enter the column names (c of them), each name seperated by a comma" << endl; char colNames[100]; cin >> colNames; for (int i = 0; i < 99; ++i){ if (colNames[i] == ','){ //parse the string, assign that string name to that column }...
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(?, ?, ?, ?);   ...
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 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...
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 =...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT