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;