在STACK中输入不同的数据类型值时无限循环

时间:2014-12-31 13:50:49

标签: c++ algorithm stack

以下是STACK的代码,没有溢出,因为我使用的是矢量概念。只要我按整数,程序似乎工作正常,但只要我按浮点值(进行测试),输出屏幕就会进入无限循环而调试器没有甚至停在 cin 语句,它只是跨越它而不将控件提供给控制台窗口。

#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();
}

1 个答案:

答案 0 :(得分:0)

输入 int 值后,例如 1.1(无效的int值)。输入转换错误发生,由于无效数据导致的输入流忽略了操作的其余部分,即使是 cin 。使代码更通用,以便处理 float或double或char