In: Computer Science
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;
}
}
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;
}
}
}