i can't get an output from this - c++

#include<iostream>
using namespace std;
class stack
{
int size=10;
int stack[size]={0}, value=0, top;
top=size;
public:
void push(int v)
{
if(top==0)
cout<<"\nstack is full\n";
else
{--top;
stack[top]=v;}
}
void pop()
{
if(top==size)
cout<<"\nstack is empty\n";
else
{top++;
stack[top];
stack[top-1]=0;
}
}
void display()
{
if(top==size)
cout<<"\nstack empty\n";
else
{
for(int i=top;i<size-1;i++)
{
cout<<stack[i];
}
}
}
};
int main()
{
stack s;
char t;
int value,ch;
do
{
cout<<"\n1.push\n";
cout<<"\n2.pop\n";
cout<<"\n3.display\n";
cout<<"enter choice:\n";
cin>>ch;
switch(ch)
{
case 1:cout<<"\nenter the value to be pushed\n";
cin>>value;
s.push(value);
break;
case 2:s.pop();
break;
case 3:s.display();
break;
default:
cout<<"\nwrong choice\n";
}
cout<<"\ndo u want to retry\n";
cin>>t;
}while(t=='y' || t=='Y');
return 0;
}

Simplest fix to errors occurring is changing int size=10; to static const int size=10;.
After this, apart from occurring warning with stack[top]; being empty statement, there is logical error in display loop in for(int i=top;i<size-1;i++) where it should be either for(int i=top;i<size;i++) or for(int i=top;i<=size-1;i++).

As answered by Tomáš Zahradníček, you need to fix a few things to have your code compile (using -std=c++11).
I used for(int i=top; i<size; ++i) in the display method. I also add that your pop method could simply do top++; without overwriting the stack.
Anyways, regarding your problem of nothing being printed on cout : you obviously tried with 1 item pushed in the stack, but not with 2, which would have pointed to faulty line (the for loop).

Related

My st.show on stack cannot be display for some reason

My problem on my code is when i run it, it says the error no matching function call to stack::show() which i have. i dont know whats causing the error, did some research that it should be on the public class which already there.
I used switch case 1 is to input 2nd is to show or to display the user inputs which I cant call out using st.show();
private:
int MAX;
int top;
string *arr_q;
public:
Stack (int size)
{
MAX=size;
top=-1;
arr_q=new string[MAX];
}
void push(string subs)
{
if ((top+1)==MAX)
cout << "Stack Overflow..." << endl;
top=top+1;
arr_q[top]=subs;
}
void show(int lim)
{
int i = lim;
cout<<"Stack contains --"<<endl;
for (int ctr=i; ctr>=0; ctr--)
{
if (ctr==i)
cout<<"\t\t"<<arr_q[ctr]<<"<--Top of Stack"<<endl;
else
cout<<"\t\t"<<arr_q[ctr]<<endl;
}
}
};
case 2: {
st.show();
break;
The error is that your show function has a parameter but when you call it there is no parameter.
Seems reasonably likely that show should be written without a parameter. Like this
void show()
{
cout<<"Stack contains --"<<endl;
for (int ctr=top; ctr>=0; ctr--)
{
if (ctr==top)
cout<<"\t\t"<<arr_q[ctr]<<"<--Top of Stack"<<endl;
else
cout<<"\t\t"<<arr_q[ctr]<<endl;
}
}

Program is running but the expected output is not showing [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed last year.
Improve this question
#include<conio.h>
#include<stdio.h>
#include<iostream>
using namespace std;
class binary_search
{
public:
int a[10],flag;
int n,i,j,index,num,temp,mid,low,high;
void getdata();
void search();
void sort_array();
};
void binary_search::sort_array()
{
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(a[j]>a[j+1])
{
temp=a[j+1];
a[j+1]=a[j];
a[j]=temp;
}
}
}
}
void binary_search::getdata()
{
cout<<"number of array "<<"\n";
cin>>n;
cout<<"\nEnter array : "<<"\n";
for(i=0;i<n;i++)
{
cin>>a[i];
}
sort_array();
cout<<"\nSorted Array Elements: ";
for(i=0;i<n;i++)
{
cout<<a[i];
}
}
void binary_search::search()
{
cout<<"\nEnter value to search: ";
cin>>num;
low=0;
high=n-1;
while(low<=high)
{
mid=(low+high)/2;
if(a[mid]==num)
{
cout<<"\nNumber is found at position "<<mid;
break;
}
else if(a[mid]>num)
{
high=mid-1;
}
else if(a[mid]<num)
{
low=mid +1;
}
else if(a[mid]!=num)
{
flag=false;
}
}
if(!flag)
{
cout<<"\nNumber is not found!!!";
}
}
int main()
{
binary_search b;
b.getdata();
b.search();
getch();
return 0;
}
Program is running but the expected output is not showing.
I'm not getting the message of "not found the number". I think I am missing something if any guidance I will get it will be awesome. Someone refer me to remove conion.h and getch() ; but still the output is not showing as expected.
Actually it's a code for binary search in C++
You must be facing problem in the test cases where, number of elements in array is exactly equal to 10. Because, if you look closely, the sort_array() that you have made have some logical error.
This is your sort_array() function which is using bubble sort technique.
void binary_search::sort_array()
{
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(a[j]>a[j+1])
{
temp=a[j+1];
a[j+1]=a[j];
a[j]=temp;
}
}
}
}
But, consider the case if n = 10, at this point int the inner loop code will try to access a[10], which is not present. So, it will go out of bound which will lead to undesirable results. So, try changing your function to the one below.
void binary_search::sort_array()
{
for(int i=0;i<n - 1;i++)
{
for(int j=0;j<n - i - 1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j+1];
a[j+1]=a[j];
a[j]=temp;
}
}
}
}
Apart from this remove #include<conio.h> and getch()

segmentation fault while using two different stack of same class in a program

i just started c++ and coded a program to push and pop in 2 stacks simultaneously inn a program....i coded it corectly but while i run the program and try to access the first stack i.e s1 it shows segmentation fault but i am able access my second stack s2 very perfectly.....help me
#include<iostream>
using namespace std;
#define max 10
class stack
{
private:
int arr[max],top;
public:
void init()
{
int top=0;
}
void push(int a)
{
arr[top++]=a;
}
int pop()
{
return arr[--top];
}
int isempty()
{
if(top==0)
return 1;
else
return 0;
}
int isfull()
{
if(top==max)
return 1;
else
return 0;
}
};
int main()
{
int a,z,cas;
stack s1;
stack s2;
s1.init();
s2.init();
while(1)
{
cout<<"Enter your choice i.e. :\n";
cout<<"1.Pushing in stack s1.\n";
cout<<"2.Pushing in stack s2.\n";
cout<<"3.Poping from stack s1.\n";
cout<<"4.Poping from stack s2.\n";
cout<<"5.To STOP.\n";
cin>>cas;
switch(cas)
{
case 1:
cout<<"Enter the number to push in stack s1:\n";
cin>>a;
if(s1.isfull()==0)
s1.push(a);
else
cout<<"The Stack is full.\n";
break;
case 2:
cout<<"Enter the number to push in stack s2:\n";
cin>>a;
if(s2.isfull()==0)
s2.push(a);
else
cout<<"The Stack is full.\n";
break;
case 3:
if(s1.isempty()==0)
cout<<"The number poped out is :\n"<<s1.pop()<<endl;
else
cout<<"The stack is empty.\n";
break;
case 4:
if(s2.isempty()==0)
cout<<"The number poped out is :\n"<<s2.pop()<<endl;
else
cout<<"The stack is empty.\n";
break;
case 5:
cout<<"The elements in stack s1 are :\n";
while(!s1.isempty())
cout<<s1.pop()<<" ";
cout<<endl;
cout<<"The elements in stack s2 are :\n";
while(!s2.isempty())
cout<<s2.pop()<<" ";
cout<<endl;
exit(1);
}
}
return 0;
}
Much more useful than a debugger is the art of reading carefully:
void init()
{
int top=0;
}
declares a local variable top.
The member variable is left uninitialised, leading to Undefined Behaviour.
That one of the stacks appears to work is just bad luck.
Making it a proper assignment:
void init()
{
top=0;
}
would do it, but this is C++, so you should use a constructor:
class stack
{
public:
stack();
// ...
};
stack::stack()
: top(0)
{
}
and then
stack s1;
stack s2;
while(1)
{
// ...

menue driven program to perform the following queue operation using array en-queue, de-queue, count the number of elements and display in c++?

i need to make a C++ program for
menu driven program to perform the following queue operation using array en-queue, de-queue, count the number of elements and display in c++?
how to make this one ?
im very weak in c++ can anyone guide me or help me or link me to a complete program to study it and understand it?!!!
i tried but i coudnt do it so i really need help
is this right or not ?
#include<iostream.h>
#include<conio.h>
void push(int st[],int data,int &top); //declaring a push class
void disp(int st[],int &top); //declaring display class
int pop(int st[],int &top); //declaring a pop class
int flg=0;
int top=-1,tos=-1;
int st[50];
void push(int st[],int data,int &top) //push
{
if(top==50-1)
flg=0;
else
{
flg=1;
top++;
st[top]=data;
}
}
int pop(int st[],int &top) //pop
{
int pe;
if(top==-1)
{
pe=0;
flg=0;
}
else
{
flg=1;
pe=st[top];
top--;
}
return(pe);
}
void disp(int st[],int &top) //display
{
int i;
if(top==-1)
{
cout<<"\nStack is Empty";
}
else
{
for(i=top;i>=0;i--)
cout<<"\t"<<st[i];
}
}
void main()
{
int dt,opt; // declare varible
int q=0;
clrscr();
cout<<"\t\t\tStack operations";
cout<<"\n\n\tMain Menu.........";
cout<<"\n\n1.Push";
cout<<"\n\n2.Pop";
cout<<"\n\n3.Exit";
cout<<"\n\n4.display";
do // useing do while for to make choice and select any options
{
cout<<"\n\n\tEnter Your Choice 1-4:"; //entering your choice
cin>>opt;
switch(opt)
{
case 1:
cout<<"\nEnter the Element to be Push:";
cin>>dt;
push(st,dt,tos);
if(flg==1)
{
cout<<"the push is done";
if(tos==50-1)
cout<<"\nStack is Now Full";
}
else
cout<<"\nStack Overflow Insertion Not Possible";
break;
case 2:
dt=pop(st,tos);
if(flg==1)
{
cout<<"\n\tData Deleted From the Stack is:"<<dt;
cout<<"\n \t pop is done";
}
else
cout<<"\nStack Empty,Deletio Not Possible:";
break;
case 3:
q=1;
break;
default:
cout<<"\nWrong Choice Enter 1-3 Only";
case 4:
disp(st,tos);
break;
}
} while(q!=1);
}
There is a queue collection in the STL library which provides all of the functionality required above for you, if for some reason you are not allowed to use this then I suggest the following logic might be helpful
when an item is popped from the front of the queue all other items must be copied down 1 element, use a for loop for this
E.g
for (int index = 1; index < arraySize; index++)
{
if (item[index] == -1)
{
item[index-1] = -1;
break;
}
item[index - 1] = item[index];
}
when an element is deleted, all items that follow that item in the queue must be moved down 1 space, find the index of the element being deleted and use a for loop
E.g
for (int index = deletedItemIndex; index < arraySize; index++)
{
if (item[index] == -1)
break;
item[index] = item[index + 1];
}
when an item is added to the queue it is simply placed at the end of the queue, but not necessarily the end of the array (perhaps initialise all the array elements with -1 to start, that way you can easily test if you are at the end of the queue)

Stack-based palindrome checker

i have a problem with my program. It should be program that recognize palindome through the stack. Everything works great, only thing that don't work is printing stacks(original and reversed) after the funcion is done.
Here is my entire code, and the problem is at case d and e:
#include <iostream>
using namespace std;
const int MAXSTACK = 21;
class stack {
private:
int stop;
char stk[MAXSTACK];
public:
stack();
~stack();
stack(const stack& s);
void push(const char c);
char pop();
char top(void);
int emptystack(void);
int fullstack(void);
void stack_print(void);
int stack::create(void);
};
stack::stack()
{
stop = 0;
}
stack::~stack() { }
stack::stack(const stack& s)
{
stop = s.stop;
strcpy(stk,s.stk);
}
void stack::push(const char c)
{
stk[stop++] = c;
}
char stack::pop()
{
return stop--;
}
char stack::top(void)
{
return stk[stop - 1];
}
int stack::emptystack(void)
{
return !stop;
}
int stack::fullstack(void)
{
return stop == MAXSTACK;
}
void stack::stack_print(void)
{
for (int i=0; i<stop; i++)
cout<<stk[i];
cout<<endl;
}
int stack::create(void)
{
return !stop;
}
char menu()
{
char volba;
cout<<"\n";
cout<<" **********.\n";
cout<<"\n";
cout<<" a ... make new containers\n";
cout<<" b ... delete content\n";
cout<<" c ... enter string\n";
cout<<" d ... print on screen first stack\n";
cout<<" e ... print on screen first stack\n";
cout<<" f ... is it palindrom\n";
cout<<" x ... exit\n";
cout<<"\n your choice : ";
cin >> volba;
return volba;
}
int main() {
char palindrome[MAXSTACK];
char volba;
stack original,reversed;
int stackitems = 0,i;
//cin.getline(palindrome,MAXSTACK);
do{
volba = menu();
switch (volba)
{
case'a':
{
original.create();
reversed.create();
cout<<"done'";
break;
}
case'b':
{
original.emptystack();
reversed.emptystack();
cout<<"empty";
break;
}
case'c':
{
cout<<"enter your string"<<endl;
cin.get();
//cin.get();
cin.getline(palindrome,MAXSTACK);
for(int o = 0; o < strlen(palindrome); o++)
if (isalpha(palindrome[o]))
{
original.push(tolower(palindrome[o]));
stackitems++;
}
original.stack_print();
break;
}
case'd':
{
original.~stack();
for(int g = 0; g < strlen(palindrome); g++)
original.push(tolower(palindrome[g]));
original.stack_print();
}
/*//cin.getline(palindrome,MAXSTACK);
for(int g = 0; g < strlen(palindrome); g++)
if (isalpha(palindrome[g]))
{
original.push(tolower(palindrome[g]));
stackitems++;
}
}
original.stack_print();*/
break;
/*{
cout<<"original: ";
original.stack_print();
break;
}*/
break;
case'e':
{
cout<<"reversed:"<<endl;
for( i = 0; i < stackitems; i++) {
reversed.push(original.top());
original.pop();
}
reversed.stack_print();
}
break;
case'f':
{
for( i = 0; i < stackitems / 2; i++) {
reversed.push(original.top());
original.pop();
}
if (stackitems % 2)
original.pop();
while (!original.emptystack()) {
if (original.top() != reversed.top()) break;
original.pop(); reversed.pop();
}
if (original.emptystack())
cout << "it is palindrom\n";
else
cout << "not palindrom\n";
break;
}
default:cout<<"!??!";
}
} while(volba!='x');
}
You've explicitly called your stack's destructor. There is almost never a good reason to do this. If the stack is a local ("on the stack", hee hee), the compile will do it for you. If it's on the heap, created with new, call delete on it, which will cause the compiler to call the destructor.
case'd':
{
original.~stack();
You have commented palindrome reading :)
//cin.getline(palindrome,MAXSTACK);
There are a few things I would like to respond with. First, I think GMan, tpdi, and Vinay all have good points. This FAQ explains why calling the destructor on a local variable is a bad idea.
I realize this is just a simple homework problem and you are probably just trying to keep your stack class lightweight, but you might consider using a container class instead of an array of characters in your stack class.
Next, I'm not sure your emptystack and create functions are doing what you think they are doing. When you declare your original and reversed stack classes in the main program the memory is allocated for your internal character array. It's not really necessary in this case to have a create function. Perhaps if you were allocating memory on the heap for your character array, you would put that code into the create function (if you chose to leave it out of the constructor for some reason), but that's not the case here.
Similarly, emptystack isn't really doing anything. It would be better to have empty stack set the stop member variable to 0. At least that way the stack would appear to be empty the next time someone tried to use it.
There's a lot more that could be said about this class, but it might be better if you tried some of the suggestions here like using the std::stack and debugging. This is, after all, your homework assignment: it will help you a lot more in the future if you find the solution yourself!