76 270
Assignments Done
Successfully Done
In May 2019

Answer to Question #52121 in C++ for Fragrance

Question #52121
Using the STL stack
In a given company an employee receives successive quantities of wheat and puts them in a containers of 20kg .Let us suppose that once a container is full , it is closed and then a new container is used. We want to write a C++ program that stimulate the work of employee . The program will ask the user to enter consecutive quantities of wheat . These quantities will be stored in a stack . Once 20kg of wheat is gathered the existing elements in the stack are removed . The stack will keep only the remaining quantity that could not be put in the container. The program will continue asking to enter new quantities until 5 containers are filled .
Expert's answer
#include <iostream>
#include <stack>
using namespace std;
int main() {
    stack<int> rest;
    int containerNumber = 1;
    int containerSum = 0;
    int quantity;
    while (containerNumber <= 5) {
        cout << "Enter quantity: ";
        cin >> quantity;
        quantity = rest.top();
        if (quantity + containerSum <= 20) {
            containerSum += quantity;
        } else {
        if (containerSum == 20) {
            cout << "Container " << containerNumber << " is filled." << endl;
            containerSum = 0;
            while (!rest.empty()) {
    return 0;

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!


No comments. Be first!

Leave a comment

Ask Your question

Privacy policy Terms and Conditions