Stacks

I am having a problem with how i should go about this. I have a tube and Im filling it up. Im having an issue figuring out how to write ... If there is room in the tube, store the gumball in the tube and print a message giving the gumball's color and saying it has been stored. If there is no room for this new gumball, print a message saying there is no room for another gumball (but the program will continue processing input requests).

I don't know where is the best place to do these requests. It keeps overlapping.

My Main is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
int main()
{
    Stack gumball;
    char choice;
    string color;
    bool choice_flag = true;
    bool t;

    do {
        cin >> choice;
        cin >> color;
        switch(choice)
        {
            case 'b':
            case 'B':
            cout << "A" << " " << color << " gumball has been bought." << endl << endl;
            gumball.push(1);
            cout << "The gumball is " << "" << color << " and has been stored." << endl << endl;
            break;
        }
    } while(choice_flag);

    return 0;
}


.cpp file (i have included #include <iostream> and using namespace std in this file to be able to print to the console.

1
2
3
4
5
6
7
8
9
10
11
// Function to add item x to stack
void Stack::push(int x)
{
    if(top == 6)
        cout << "There is no room for another gumball." << endl << endl;
    else
    {
        top++;
        gumballs[top] = x;
    }
}


Any suggestions will be appreciated. Thank u.
closed account (zwA4jE8b)
your problem does not seem to be with the stack but that you never change 'choice_flag' to false, so you loop never exits.
Last edited on
I know that but that wasn't the problem at the moment. The first issue i am is with the push itself. Even after i pushed 10 items in the stack, it would still allow for more instead of saying there is no more room.

For example:

main ():

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
int main()
{
    Stack gumball;
    char choice;
    string color;
    bool choice_flag = true;
    bool t;

    do {
        cin >> choice;
        cin >> color;
        switch(choice)
        {
            case 'b':
            case 'B':
            cout << "A" << " " << color << " gumball has been bought." << endl << endl;
            gumball.push(1);
            gumball.push(1);
            gumball.push(1);
            gumball.push(1);
            gumball.push(1);
            gumball.push(1);
            gumball.push(1);
            gumball.push(1);
            gumball.push(1);
            cout << "The gumball is " << "" << color << " and has been stored." << endl << endl;
            break;
            case 'q':
            case 'Q':
                choice_flag = false;
                break;
        }
    } while(choice_flag);

    return 0;
}


After i run this, it still allows more to be stored. I know something is wrong with my syntax to get the result I want but im confused of how to change it
Your code on line 26 doesn't have any condition as to whether the gumball was actually successfully stored or not. The class itself isn't adding any more gumballs to the stack.
Ok I see. But how do i get the main to be notified from the .cpp method function if the function is a void, therefore it does not return anything??
closed account (zwA4jE8b)
1
2
3
4
5
template <class dType>
bool stack<dType>::isEmpty() const{	return (_top < 0);}

template <class dType>
bool stack<dType>::isFull() const{ return (_top >= MAXSTACK);}



you can use functions like this. MAXSTACK is the self explanatory.

edit: oh and then in your main function you can check if isfull ? choiceflag = false;
or whatever, cout a message saying your gumballs are full.
Last edited on
Topic archived. No new replies allowed.