Hi ,all
I hope that everybody will be fine
I have q;
How do I write simple [very simple] code that receives letters with stack [push,pop,over flow,under flow]
for example .
output:
Enter later:
A
B
C
D
E
EDCBA

i writed code that receives number .. but i face problem when I coded to be receives latter ..
I'll be happy with your help

What have you done so far?? If you are fine with nos...chars are not that different. Post the code that you have written so far.

#include<iostream.h>
const int MAXSTACK=5;
const int TRUE=1;
const int FALSE=0;
// Class Declaration
class Stack
{
private:
	int top;
	int num[MAXSTACK];
public:
	Stack(); // constructor
	void push(int);
	int pop();
	int isempty();
	int isfull();
};
// implementation section
Stack::Stack()
{
	top=-1; // initialize the top-of-stack position current position
}
void Stack::push(int value)
{
	top++; // increment the index in top
	num[top]=value; // store the value
}

int Stack::pop()
{
	int topval;
	topval=num[top]; // retrieve the top element
	top--; // decrement the index stored in top
	return(topval);
}
int Stack::isempty()
{
	if(top==-1)
	return TRUE;
	else
	return FALSE;
}

int Stack::isfull()
{
	if(top==MAXSTACK-1)
	return TRUE;
	else
	return FALSE;
}
//
int main()
{
	Stack digits; // define a Stack named digits
	int newnum;
	cout<<"Enter as many digits wish.. \n";
	while(1)
{
	cout<<"Enter a digit: ";
	cin>>newnum;
	if(digits.isfull()) //check overflow
{
	cout<<"\nNo more storage allocation space."
	    <<"\nThe last digit has not been entered on the stack.";
	break;
}
	else
	digits.push(newnum); // push value onto the stack
}
// pop and display digits from the stack.
	cout<<"\nThe values popped from the stack are: \n";
	while(!digits.isempty()) // check the underflow
{
	cout<<digits.pop()<<"\n";
}
	return 0;
}

I don't think is like this
because i try many thing until i say tha problem in my computer .
and I also want code with( iostream) library just .

great. The code though a bit bad in english, is not bad except for that iostream.h thing. for using characters (or strings if you want to have more than 1 character) just replace the ints to strings at appropriate places.

// may 10 2010


#include <iostream>
#include <string>

using namespace std;

const int MAXSTACK=5;
const int TRUE=1;
const int FALSE=0;
// Class Declaration
class Stack
{
private:
	int top;
	string num[MAXSTACK];  //change1
public:
	Stack(); // constructor
	void push(string);
	string pop();        //change2
	int isempty();
	int isfull();
};
// implementation section
Stack::Stack()
{
	top=-1; // initialize the top-of-stack position current position
}
void Stack::push(string value)   //change3
{
	top++; // increment the index in top
	num[top]=value; // store the value
}

string Stack::pop()
{
	string topval;      //change4
	topval=num[top]; // retrieve the top element
	top--; // decrement the index stored in top
	return (topval);
}
int Stack::isempty()
{
	if(top==-1)
	return TRUE;
	else
	return FALSE;
}

int Stack::isfull()
{
	if(top==MAXSTACK-1)
	return TRUE;
	else
	return FALSE;
}
//
int main()
{
	Stack digits; // define a Stack named digits
	string newnum;    //change6, i should have changed the name as well. but     felt too lazy!!
	cout<<"Enter as many letters wish.. \n";
	while(1)
{
	cout<<"Enter a char: ";
	cin>>newnum;
	if(digits.isfull()) //check overflow
{
	cout<<"\nNo more storage allocation space."
	    <<"\nThe last digit has not been entered on the stack.";
	break;
}
	else
	digits.push(newnum); // push value onto the stack
}
// pop and display digits from the stack.
	cout<<"\nThe values popped from the stack are: \n";
	while(!digits.isempty()) // check the underflow
{
	cout<<digits.pop()<<"\n";
}
	return 0;
}

hope it helps.

can we write that code without (#include <string>)
because I don't study it and I dont't now what it use for ?
THank you for your Help ...:).

Sorry for the late reply.

Sure...i included string for more flexibility. If you don't want to use that then simply use char instead of string. Just replace all the instances of the word "string" with "char" and remove that #include <string> thingy.
That's it.
Hope it helps.

string is included by iostream, so you can just omit the include and still use strings.

commented: be 100% sure, atleast before advising. +0

string is included by iostream, so you can just omit the include and still use strings.

That is not always true. It is far better to be explicit. There is less chance for an issue.

well actually <string> includes all the functions and other instances and also overload operators(to enhance functionality) of the string "class". So if you wanna use any of those(which in all probability you will) you HAVE to include <string>.
In the above code as well, you HAVE to #include <string>. Why ?? Because I have used the << and >> operators for string objects and they will not work unless you #include <string>.
So its always a better idea to include <string> as Fbody has already mentioned.

Hope it clarifies
Cheers.

Thank for all
I solved my problem with a char <<as will as I want
without [include<string>]:)
,also what you say i think is true and more easy ,& save your memory
Thank you

great....in that case, just mark this thread "solved".

happy coding

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.