0% found this document useful (0 votes)
418 views4 pages

Cs301 Assignment 1 Solution Vu360

The document details a C++ program that implements a shopping list class with methods to add, remove, view, and clear items from the list. The program uses a class with private array and count properties to store list items, and public methods to manipulate the list. A main function tests the class with a menu driven program.

Uploaded by

nabenop962
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
418 views4 pages

Cs301 Assignment 1 Solution Vu360

The document details a C++ program that implements a shopping list class with methods to add, remove, view, and clear items from the list. The program uses a class with private array and count properties to store list items, and public methods to manipulate the list. A main function tests the class with a menu driven program.

Uploaded by

nabenop962
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Cs301 Assignment 1 Solution

#include <iostream>

#include <string>

using namespace std;

class ShoppingList {

private:

string items[100];

int itemCount;

public:

ShoppingList() {

itemCount = 0;

void addItem(string item) {

if (itemCount < 100) {

items[itemCount] = item;

itemCount++;

} else {

cout << "Shopping list is full. Cannot add more items." << endl;

void removeItem(string item) {


bool found = false;

for (int i = 0; i < itemCount; i++) {

if (items[i] == item) {

found = true;

for (int j = i; j < itemCount - 1; j++) {

items[j] = items[j + 1];

itemCount--;

break;

if (!found) {

cout << "Item not found in the shopping list." << endl;

void viewList() {

if (itemCount == 0) {

cout << "Shopping list is empty." << endl;

} else {

cout << "Current items in the shopping list:" << endl;

for (int i = 0; i < itemCount; i++) {

cout << items[i] << endl;

void clearList() {

itemCount = 0;
}

};

int main() {

ShoppingList shoppingList;

int choice;

string item;

do {

cout << "1. Add item to the shopping list." << endl;

cout << "2. Remove item from the shopping list." << endl;

cout << "3. View current items in the shopping list." << endl;

cout << "4. Clear the shopping list." << endl;

cout << "5. Exit the program." << endl;

cout << "Enter your choice: ";

cin >> choice;

switch (choice) {

case 1:

cout << "Enter item to add: ";

cin.ignore();

getline(cin, item);

shoppingList.addItem(item);

break;

case 2:

cout << "Enter item to remove: ";

cin.ignore();

getline(cin, item);

shoppingList.removeItem(item);
break;

case 3:

shoppingList.viewList();

break;

case 4:

shoppingList.clearList();

cout << "Shopping list cleared." << endl;

break;

case 5:

cout << "Exiting the program." << endl;

break;

default:

cout << "Invalid choice. Please choose a valid option." << endl;

cout << endl;

} while (choice != 5);

return 0;

You might also like