Need help with program templating

I need to be able to input either a int or string and perform several different actions that are mentioned in the case switch down below. I am not sure what I am doing wrong completely and I've gotten it close to working a few times I think. I would really appreciate any help.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include <iostream>
//#include "Stack"
#include <string>
#include <cstdlib>
#define SZ 10000

using namespace std;

template <class T>
class STACK
{
private:
	T num[SZ];
	T top;
	int ze;
public:
	STACK(); //default constructor
	T push(int); // will push items
	T pop(); // will pop items
	T popBack(); //will pop item at back of stack
	T peek(); // will peek
	T isEmpty(); // will check if stack is empty
	T displayItems(); // will display items user has entered
};

template <class T>
STACK<T>::STACK()
{
	top = -1;
}

template <class T>
T STACK<T>::isEmpty()
{
	if (top == -1)
		return 1;
	else
		return 0;
}

template <class T>
T STACK<T>::push(int n)
{
	++top;
	num[top] = n;
	return n;
}

template <class T>
T STACK<T>::pop()
{
	//to store and print which number is deleted
	T tem;
	//check for empty
	if (isEmpty())
		return 0;
	tem = num[top];
	--top;
	return tem;
}

template <class T>
T STACK<T>::popBack()
{
	//to store and print which number is deleted
	T tem;
	//check for empty
	if (isEmpty())
		return 0;
	tem = num[top];
	++top;
	return tem;
}

template <class T>
T STACK<T>::peek()
{
	
	if(isEmpty())
	{
		cout << "The stack is empty" << endl;
		return NULL;
	}
	else
	{
		return top == num;
	}
		
}

template <class T>
T STACK<T>::displayItems()
{
	int inc; //for loop

	cout << "Stack is ";

	for (inc = (top); inc >= 0; inc--)
		cout << num[inc] << " " << endl;
}

//Main has int argc and charargv(might or might not work)
int main()
{
	//declare object stack for class Stack
	//int that will be used online in main
	STACK <T> staack;
	int userChoice;
	T userInput;
	T tem;

	do
	{
		//These are the first lines of code that will be prompted to the user
		cout << "Welcome to this simple push, pop, peek program" << endl;
		cout << "Push Item(3 max) => 1" << endl;
		cout << "Pop Item(s) => 2" << endl;
		cout << "Pop last item from stack => 3" << endl;
		cout << "Peek Item at top of stack = > 4" << endl;
		cout << "Display Items => 5" << endl;
		cout << "Exit the Program => 6" << endl;
		cout << "Enter your choice: " << " "<< endl;
		cin >> userChoice;

		//switch and case are the easiest options for any user
		// choice menu and the single number being entered makes
		// it easier.
		switch (userChoice)
		{
		case 1:
			cout << "Enter number to insert: ";
			getline(cin, userInput);
			tem = staack.push(userInput);
			if (tem == 0)
				cout << "Stack is full" << endl;
			else
				cout << "The number "<< tem << " inserted" << endl;
			break;

		case 2:
			tem = staack.pop();
			if (tem == 0)
				cout << "STACK IS EMPTY." << endl;
			else
				cout << "The number "<< tem << " is removed." << endl;
			break;

		case 3:
			tem = staack.pop_back();
			if (tem == 0)
				cout << "STACK IS EMPTY." << endl;
			else
				cout << "Last element "<< tem << " from stack popped" << endl;
			break;

		case 4:
			cout << "Feature not ready yet" << endl;
			tem = staack.peek();
			break;

		case 5:
			staack.displayItems();
			break;

		case 6:
			//exit();
			break;

		default:
			cout << "This choice is invalid, try another one" << endl;
		}
	} while (userChoice != 0);

	return 0;
}[code]
[/code]
Last edited on
1. Use code tags when you post code.
http://www.cplusplus.com/articles/jEywvCM9/

> STACK <T> staack;
> int userChoice;
> T userInput;
> T tem;
In your templates, T is just a placeholder name for some type as yet to be specified.

When you want an actual stack, you say what type you want.
So to get a stack of int's.
1
2
3
4
STACK <int> staack;
int userChoice;
int userInput;
int tem;


What if I want the user to be able to input either a int or string?
would I just have to give the user the option to enter a string or int?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>

int main()
{
   std::string input { };

   std::cout << "Enter a number or a word: ";
   std::getline(std::cin, input);
   std::cout << '\n';

   std::cout << "You entered: " << input << '\n';

   try
   {
      std::stoi(input);
      std::cout << "You entered a number.\n";
   }
   catch (...)
   {
      std::cout << "You entered a string.\n";
   }
}

Enter a number or a word: one

You entered: one
You entered a string.
Enter a number or a word: 127

You entered: 127
You entered a number.
Topic archived. No new replies allowed.