Q:

Infinite Loop while inputting the different data type values in STACK

Below is the code for STACK with no overflow since I am using vector concept. Program seems to be working fine as long as I push integers, but as soon as I push float value (for testing) the output screen goes to infinite loop and debugger doesn't even stop at cin statement and it simply crosses it without giving the control to the console window. 
#include"iostream"
class Mystack
{
private:
    int *input;
    int top;
    int capacity;
public:
    Mystack();
    ~Mystack();
    void push(int x);
    int pop();
    int topElement() const;
    bool isEmpty() const;
    void print();
};
Mystack::Mystack()
{
    top = -1;
    capacity = 5;
    input = new int[capacity];
}
Mystack::~Mystack()
{
    delete[]input;
}
void Mystack::push(int x)
{
    if (top + 1 == capacity)
    {
        int *vec = new int[capacity+capacity];
        for (int i = 0; i <=top; i++)
        {
            vec[i] = input[i];
        }
        input = vec;
        capacity = capacity * 2;
        top++;
        input[top] = x;
    }
    else
    {
        top++;
        input[top] = x;
    }
}
int Mystack::pop()
{
    if (isEmpty())
    {
        throw std::out_of_range("Stack Underflow");
    }
    else
    { 
        std::cout << "The popped element is" << input[top];
        return input[top--];

    }
}
bool Mystack::isEmpty() const
{
    if (top == -1)
    {
        std::cout << "Is Empty" << std::endl;
        return true;
    }
    else
    {
        std::cout << "Not Empty" << std::endl;
        return false;
    }
}
int Mystack::topElement() const
{
    if (top == -1)
    {
        throw std::out_of_range("No Element to Display");
    }
    else
    {
        std::cout << "The top element is : " << input[top];
        return input[top];
    }
}
void Mystack::print()
{
    for (int i = 0; i <= top; i++)
    {
        std::cout << input[i] << " ";
    }
}
int main()
{
    Mystack s1;
    int num, ch = 1;
    while (ch >0)
    {
        std::cout << "\n1. PUSH" << std::endl;
        std::cout << "2. TOP" << std::endl;
        std::cout << "3. IsEmpty" << std::endl;
        std::cout << "4. POP" << std::endl;
        std::cout << "5. EXIT" << std::endl;
        std::cout << "6. Print" << std::endl;
        std::cout << "Enter the choice" << std::endl;
        std::cin >> ch;  // DEBUGGER DOES NOT GIVE CONTROL TO CONSOLE WINDOW AND ASK FOR INPUT ONCE I PUT FLOAT VALUES, IT SIMPLE CROSSES IT
        switch (ch)
        {
        case 1:
            std::cout << "Enter the number to be pushed" << std::endl;
            std::cin >> num;
            s1.push(num);
            break;
        case 2:
            std::cout << "Get the TOP Element" << std::endl;
            try
            {
                s1.topElement();
            }
            catch (std::out_of_range &oor)
            {
                std::cerr << "Out of Range error:" << oor.what() << std::endl;
            }
            break;
        case 3:
            std::cout << "Check Empty" << std::endl;
            s1.isEmpty();
            break;
        case 4:
            std::cout << "POP the element" << std::endl;
            try
            {
                    s1.pop();
            }
            catch (const std::out_of_range &oor)
            {
                std::cerr << "Out of Range error: " << oor.what() << '\n';
            }
            break;
        case 5: exit(0);
        case 6:
            s1.print();
            break;
        default:
            std::cout << "Enter a valid input";
            break;
        }
    }
    std::cin.get();
}

A:

Once you input the int value for example as 1.1 (invalid int value). Input conversion error occurs and the input stream  because of invalid data ignores the rest of the operation even the cin.  Make your code more generic in order for it to handle float or double or char.

