Click here to Skip to main content
15,887,264 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How do I make a C++ program that will store names(string or char) using stack:
For example: The output will be like this

Enter name: john
Stack: : : john
Enter name: alice
Stack: : : john alice
Enter name: joanna
Stack: : : john alice joanna
Enter name: dave
Stack: : : john alice joanna dave
Enter name: jake
Stack: : : john alice joanna dave jake

at least 5 times the user will input names

What I have tried:

I've tried this one but this one output only integer I like to change this one to string that can read names..
C
#include <iostream>
using namespace std;
const int STACK_SIZE = 5;
//prototype
bool isEmpty(int);
bool isFull(int , int);
void push(int[], int&, int);
void print(int[], int);
int pop (int[], int&);

int main()
{
	int top =-1;
	int stack[STACK_SIZE];

	int value;
	
	do{
		cout<<"Enter an integer:";
		cin>>value;
		push(stack, top ,value);
		print (stack, top);
		
	}while(!isFull(top,STACK_SIZE));
	
	while(!isEmpty(top))
		cout<< pop(stack, top)<<"";
		
		cout<<endl;
		system ("pause");
}

bool isEmpty(int top)
{
	return (top== -1);
}
bool isFull(int top, int size)
{
	return(top ==size-1);
}
void push(int stack[], int& top, int value)
{
	top++;
	stack[top] = value;
}
int pop(int stack[], int& top)
{
	int value = stack[top];
	top--;
	
	return value;
}
void print(int stack[], int top)
{
	cout<<"Stack:::";
	for(int i=0;i<=top;i++)
	cout<<stack[i]<<" ";
	cout<<endl;
}


output:
Enter integer:2
Stack: : :2
Enter integer:2
Stack: : :2
Enter integer:2
Stack: : :2
Enter integer:6
Stack: : :2 6
Enter integer:7
Stack: : :2 6 7
Enter integer:9
Stack: : :2 6 7 9
Enter integer:3
Stack: : :2 6 7 9 3

Please help me I need this URGENT ..
Posted
Updated 21-Aug-16 6:28am
v2
Comments
Richard MacCutchan 21-Aug-16 11:00am    
There are many samples on the internet that explain how to manage a simple stack. Also sorry, but this is not urgent.
Philippe Mori 22-Aug-16 19:41pm    
What is the problem of replacing int by string at appropriate places?

1 solution

Please Note:
I've been warned here by professionals for solving HomeWorks so I don't provide the solution(source code) for your Question.


You are probably looking for Containers there are various containers in C++ STL and to my knowledge almost many provides the solution you are looking for.

Example Solved Using set[This can be done using list, deque etc and etc]

Set will be automatically sorted and could not add duplicate elements(If you needed unsorted and could duplicates then refer something about Sequence containers).
C++
set<int> newSet;
	// Insertion of values
	newSet.insert(9);
	newSet.insert(8);
	newSet.insert(1);
	newSet.insert(0);
	newSet.insert(4);
	newSet.insert(6);
	// Iterator is something which will links Algorithm and container
	// Half open and closed implementation which means itr1 will point to the first elment of the set
	// itr2 will point to the next element of the last element present in the container
	set<int>::iterator itr1 = newSet.begin();
	set<int>::iterator itr2 = newSet.end();
	//Common iterator itr this is not equal to the end because the pointer points to the next element of the 
	//last elemet present in the container which is /0 i.e null
	for (set<int>::iterator itr = itr1; itr != itr2; itr++)
	{
		cout << *itr<<endl;
	}
	// No duplicates can be inserted in Set but could be inserted in multiset,vectors,list etc.,
	pair<set><int>::iterator, bool> tell;
	tell = newSet.insert(8);
	if (tell.second == false)cout << "Element present in the set" << endl;
	cin.get();
	return 0;

</int></set></int></int></int></int>


I hope I have explained something but please refer about STL for more info.
If you have any doubts please feel free to ask me.
 
Share this answer
 
v2
Comments
Richard MacCutchan 21-Aug-16 12:40pm    
Using STL containers will not help, since the question is really asking the developer to create a stack, and learn how a stack is supposed to work. In using his/her initiative to figure this out he/she will learn far more than if one of us posts the answer. By all means offer suggestions but don't do it all for them.
[no name] 22-Aug-16 12:34pm    
Sorry sir, my mistake kindly excuse me

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900