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.
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 usingnamespace 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;
}
}
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.
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.