Answer:
#include <stdio.h>
#include <iostream>
using namespace std;
int main()
{
//varible to indicate size of the array
int size=20;
//arrays to hold grocery item names and their quantities
std::string grocery_item[size];
int item_stock[size];
//variables to hold user input
std::string itemName;
int quantity;
//variable to check if item is already present
bool itemPresent=false;
//variable holding full stock value
int full_stock=100;
for(int n=0; n<size; n++)
{
grocery_item[n]="";
item_stock[n]=0;
}
do
{
std::cout << endl<<"Enter the grocery item to be added(enter q to quit): ";
cin>>itemName;
if(itemName=="q")
{
cout<<"Program ends..."<<endl; break;
}
else
{
std::cout << "Enter the grocery item stock to be added: ";
cin>>quantity;
}
for(int n=0; n<size; n++)
{
if(grocery_item[n]==itemName)
{
itemPresent=true;
item_stock[n]=item_stock[n]+quantity;
}
}
if(itemPresent==false)
{
for(int n=0; n<size; n++)
{
if(grocery_item[n]=="")
{
itemPresent=true;
grocery_item[n]=itemName;
item_stock[n]=item_stock[n]+quantity;
}
if(item_stock[n]==full_stock)
{
item_stock[n]=item_stock[n]*2;
}
}
}
}while(itemName!="q");
return 0;
}
OUTPUT
Enter the grocery item to be added(enter q to quit): rice
Enter the grocery item stock to be added: 23
Enter the grocery item to be added(enter q to quit): bread
Enter the grocery item stock to be added: 10
Enter the grocery item to be added(enter q to quit): bread
Enter the grocery item stock to be added: 12
Enter the grocery item to be added(enter q to quit): q
Program ends...
Explanation:
1. The length of the array and the level of full stock has been defined inside the program.
2. Program can be tested for varying values of length of array and full stock level.
3. The variables are declared outside the do-while loop.
4. Inside do-while loop, user input is taken. The loop runs until user opts to quit the program.
5. Inside do-while loop, the logic for adding the item to the array and adding the quantity to the stock has been included. For loops have been used for arrays.
6. The program only takes user input for the item and the respective quantity to be added.