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.
#include <iostream>
//#include "Stack"
#include <string>
#include <cstdlib>
#define SZ 10000
usingnamespace 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;
elsereturn 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]