Question

Write in c++ please

Imagine you are writing a program to manage a shopping list. Each shopping list item is represented by a string stored in a c

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Below is the complete code as per the requirements. It is well explained inside the code using comments.

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

// prints the vector
void print(vector<string> shoppingList)
{
   cout << endl;
   for (vector<string>::const_iterator i = shoppingList.begin(); i != shoppingList.end(); ++i)
   {
       cout << *i << " ";
   }
   cout << endl;
}

// main function
int main()
{
   // create a vector of string
   vector<string> shoppingList;

   // print is it is empty
   if (shoppingList.empty())
   {
       cout << "Shopping List is empty" << endl;
   }

   // append items
   cout << endl << "Adding Items. . ." << endl;
   shoppingList.push_back("eggs");
   shoppingList.push_back("milk");
   shoppingList.push_back("sugar");
   shoppingList.push_back("chocolate");
   shoppingList.push_back("flour");
   cout << "Items added." << endl;

   // print the list
   print(shoppingList);
  

   // remove last element from vector and print it
   shoppingList.pop_back();
   cout << "Last element removed." << endl;
   print(shoppingList);

   // append "coffee" and print it
   shoppingList.push_back("coffee");
   cout << "Coffee added." << endl;
   print(shoppingList);

   // search for "sugar" and replace it with "honey"
   vector<string>::iterator it = find(shoppingList.begin(), shoppingList.end(), "sugar");
   int pos = distance(shoppingList.begin(), it);
   shoppingList.at(pos) = "honey";
   cout << "sugar replaced with honey" << endl;
   print(shoppingList);

   // clear the vector
   shoppingList.clear();
   cout << "Shopping list cleared." << endl;

   // print is it is empty
   if (shoppingList.empty())
   {
       cout << "Shopping List is empty" << endl;
   }

   return 0;
}

Below is the sample output:

Shopping List is empty Adding Items. Items added. eggs milk sugar chocolate flour Last element removed. eggs milk sugar choco

This completes the requirement. Let me know if you have any queries.

Thanks!

Add a comment
Know the answer?
Add Answer to:
Write in c++ please Imagine you are writing a program to manage a shopping list. Each...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write in C++: Please create a shopping list using the STL list container, then follow the...

    Write in C++: Please create a shopping list using the STL list container, then follow the following instructions. Create an empty list. Append the items, "eggs," "milk," "sugar," "chocolate," and "flour" to the list. Print the list. Remove the first element from the list. Print the list. Insert the item, "coffee" at the beginning of the list. Print the list. Find the item, "sugar" and replace it with "honey." Print the list. Insert the item, "baking powder" before "milk" in...

  • Q1 (2 pts) Shopping list Write a program that prompts a user for items on their...

    Q1 (2 pts) Shopping list Write a program that prompts a user for items on their shopping list and keeps prompting the user until they enter "Done" (make sure your program can stop even if the user enters "Done" with different cases, like "done"), then prints out the items in the list, line by line, and prints the total number of items on the shopping list. Output (after collecting items in the while loop) should look something like this: Apple...

  • Shopping Cart You are to write a program that reads the name of the item, the quantity of each it...

    C++ language Shopping Cart You are to write a program that reads the name of the item, the quantity of each item, and the cost per item from a file. The program will then print out the item and how much is owed for each item. After everything is read in, it will print out the total cost of the cart. Your program should have the following functions main - This function asks the user to enter a filename and...

  • A specific question I have on this project is how do I make the stars all...

    A specific question I have on this project is how do I make the stars all align into making a box. Whenever I post this question the stars on the right edge go in towards the item of the shopping list. I need the starts to create a a box around the shopping items. Instructions: Using Python write a program that allows a user to create and use a shopping list. Your program will display a menu of options to...

  • Write a program in Python that finds the index of an item in a list. For...

    Write a program in Python that finds the index of an item in a list. For example: You create a of list of colors You find the index of the color "red" Print the list and the index of "red" Your program should work for every item in your list, and it must have at least 3 items in the list. It must also work if you search for an item and it does not exist in the list. You...

  • Assignment: Write a program in C++ that allows a customer to go on-line shopping. This is...

    Assignment: Write a program in C++ that allows a customer to go on-line shopping. This is a start-up venture and the stock of our on-line company is currently limited to the following items: A gift card to Home Depot, $50.00 A bottle of cologne (The One by Dolce Gabbana), $24.00 3. Akeychainwithabathtubornament,$14.00 4. Acard,$4.00 Although all items are in stock, the customer should only be made aware of the items that he or she can afford. In addition, demand for...

  • Write a Python program (remove_first_char.py) that removes the first character from each item in a list...

    Write a Python program (remove_first_char.py) that removes the first character from each item in a list of words. Sometimes, we come across an issue in which we require to delete the first character from each string, that we might have added by mistake and we need to extend this to the whole list. Having shorthands (like this program) to perform this particular job is always a plus. Your program should contain two functions: (1) remove_first(list): This function takes in a...

  • 7.11 LAB: Online shopping cart - Part 2 This program extends the earlier "Online shopping cart" program. (Consid...

    7.11 LAB: Online shopping cart - Part 2 This program extends the earlier "Online shopping cart" program. (Consider first saving your earlier program). (1) Extend the ItemToPurchase namedtuple to contain a new attribute. (2 pts) item_description (string) - Set to "none" in the construct_item() function Implement the following function with an ItemToPurchase as a parameter. print_item_description() - Prints item_name and item_description attribute for an ItemToPurchase namedtuple. Has an ItemToPurchase parameter. Ex. of print_item_description() output: Bottled Water: Deer Park, 12 oz....

  • 11.12 LAB*: Program: Online shopping cart (continued) This program extends the earlier "Online shopping cart" pr...

    11.12 LAB*: Program: Online shopping cart (continued)This program extends the earlier "Online shopping cart" program. (Consider first saving your earlier program).(1) Extend the ItemToPurchase class to contain a new attribute. (2 pts)item_description (string) - Set to "none" in default constructorImplement the following method for the ItemToPurchase class.print_item_description() - Prints item_description attribute for an ItemToPurchase object. Has an ItemToPurchase parameter.Ex. of print_item_description() output:Bottled Water: Deer Park, 12 oz.(2) Build the ShoppingCart class with the following data attributes and related methods. Note: Some can be method stubs...

  • Write a program that incorporates these functions and statements: • list • while loop • for...

    Write a program that incorporates these functions and statements: • list • while loop • for loop • if • print() • input() • concatenation This program must create a grocery shopping list based on the user input • the program will ask a user to enter a new grocery item until the user presses Enter • then all user input will be added to a list • then the list with all grocery items will be printed

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT