#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++; cout<<item <<"added to the shopping List. \n"; } else{ cout<< "Shopping List is full.Can not Add more items. \n"; } } void removeItem(int index){ if(index-1<Itemcount){ Itemcount--; cout<<Items[index-1]<<"remove from Shopping List.\n"; } else{ cout<<index<<"index not found in the Shopping List.\n"; } for (int j=index-1;j<=Itemcount;j++){ Items[j]=Items[j+1]; } } void viewlist(){ if (Itemcount==0){ cout<<"Shopping List is empty.\n"; } else{ cout<<"Items in the Shopping List:\n"; for(int i=0;i<Itemcount;i++){ cout<<i+1<<"."<< Items[i]<< "\n"; } cout<<"\n"; } void clearlist() { Itemcount =0; cout<< "Shopping List cleared.\n"; } } }; main(){ Shoppinglist shoppinglist; int choice, index; string item; cout<<" Welcome To The Shopping List Manager\n"; do{ cout << "1. Add Item\n"; cout << "2. Remove Item\n"; cout << "3. View List\n"; cout << "4. clear List\n"; cout << "5. Exit\n"; cout << "Enter Your Choice"; cin>> choice; Switch(choice) { case 1: cout << "Enter itemd to add:"; cin>>item; shoppinglist.additem(item); break; case 2: cout << "Enter item index to remove:"; cin>> index; shoppinglist.removeItem(index); break; case 3: shoppinglist.viewlist(); break; case 4: shoppinglist.cleatlist(); break; case 5: cout << "Exiting program. Goodye!\n"; break; default: cout <<"Invalid choice.Please Try Again.\n"; } }while(choice !=5); }
Standard input is empty
#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++; cout<<item <<"added to the shopping List. \n"; } else{ cout<< "Shopping List is full.Can not Add more items. \n"; } } void removeItem(int index){ if(index-1<Itemcount){ Itemcount--; cout<<Items[index-1]<<"remove from Shopping List.\n"; } else{ cout<<index<<"index not found in the Shopping List.\n"; } for (int j=index-1;j<=Itemcount;j++){ Items[j]=Items[j+1]; } } void viewlist(){ if (Itemcount==0){ cout<<"Shopping List is empty.\n"; } else{ cout<<"Items in the Shopping List:\n"; for(int i=0;i<Itemcount;i++){ cout<<i+1<<"."<< Items[i]<< "\n"; } cout<<"\n"; } void clearlist() { Itemcount =0; cout<< "Shopping List cleared.\n"; } } }; main(){ Shoppinglist shoppinglist; int choice, index; string item; cout<<" Welcome To The Shopping List Manager\n"; do{ cout << "1. Add Item\n"; cout << "2. Remove Item\n"; cout << "3. View List\n"; cout << "4. clear List\n"; cout << "5. Exit\n"; cout << "Enter Your Choice"; cin>> choice; Switch(choice) { case 1: cout << "Enter itemd to add:"; cin>>item; shoppinglist.additem(item); break; case 2: cout << "Enter item index to remove:"; cin>> index; shoppinglist.removeItem(index); break; case 3: shoppinglist.viewlist(); break; case 4: shoppinglist.cleatlist(); break; case 5: cout << "Exiting program. Goodye!\n"; break; default: cout <<"Invalid choice.Please Try Again.\n"; } }while(choice !=5); }