Need help with my programming. I don\'t want it rewritten, I just want help corr
ID: 3567692 • Letter: N
Question
Need help with my programming. I don't want it rewritten, I just want help correcting any errors and please let me know what I am doing wrong.
#include <stack>
#include <string>
#include <iostream>
using namespace std;
class Safestack {
stack<string> s;
string top (){
public:
try
{
stack<int> values;
string input;
int c=0;
};
main()
{
SafeStack s;
s.push(string);
cout<<s.top();
s.pop();
s.top(); // will give an error
}
Explanation / Answer
The push() function is used to populate the data into the stack defined.
The function Push() is given below,
Push
----
// max is maximum number of elements
// stack[] is the array for stack
void stack::push(int y) //Push Operation
{
if(t<max)
{
t=t+1;
stack[t]=y;
}
else
cout<<endl<<"stack overflows..."<<endl;
}
The pop function is used to remove the data from the stack.
POP
-----
// max is maximum number of elements
// stack[] is the array for stack
void stack::pop() //Pop Operation
{
int item;
if(t>=0)
{
t=t-1;
item=stack[t+1];
cout<<endl<<"popped item >>"<<item<<endl;
}
else
cout<<endl<<"stack underflows"<<endl;
}
The try.. catch is used for exception handling. It is not mandatory to use the try .. catch in the program , it only enhances the error handling.
The format of try
---
try {
// code here
}
catch (int param) { cout << "int exception"; }
catch (char param) { cout << "char exception"; }
catch (...) { cout << "default exception"; }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.