Answer to Question #23757 in C++ for Rohan Saha

Question #23757
Write an interactive menu driven c++ program to implement stack using class. 1.Push an element in stack 2.Pop an element in stack. 3.Show the contents of the stack. 4.Exit
1
Expert's answer
2013-02-06T08:31:54-0500
#include <iostream>
#include <cstdlib>
#include <stack>
using namespace std;

void show_menu()
{
cout<<"Menu:"<<endl;
cout<<"1.Push an element in the stack"<<endl;
cout<<"2.Pop an element from the stack."<<endl;
cout<<"3.Show the content of the stack."<<endl;
cout<<"4.Exit"<<endl;
}

void push_option(stack<int> &container)
{
cout<<"Enter, please, an element (integer number): ";
int element;
cin>>element;
container.push(element);
}
void pop_option(stack<int> &container)
{
if(!container.empty())
{
cout<<"Removed element: "<<container.top()<<endl;
container.pop();
}
else
{
cout<<"There are no elements in the stack!"<<endl;
}
}
void show_stack_option(stack<int> &container)
{
if(!container.empty())
{
stack <int> temp_container(container);
cout<<"Content of the stack: "<<endl;
while (!temp_container.empty())
{
cout<<temp_container.top()<<endl;
temp_container.pop();
}
}
else
{
cout<<"The stack is empty!"<<endl;
}
}

int main()
{
stack<int> container;
int chosen_option;
show_menu();
bool exit = false;
while(!exit)
{
cin>>chosen_option;
system("CLS");
switch (chosen_option)
{
case 1:
push_option(container);
system("pause");
break;
case 2:
pop_option(container);
system("pause");
break;
case 3:
show_stack_option(container);
system("pause");
break;
case 4:
exit = true;
break;
default:
break;
}

system("CLS");
show_menu();

}

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!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS