首页 文章

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

提问于
浏览
0

下面是STACK的代码,没有溢出,因为我使用矢量概念 . 只要我按 integers ,程序似乎工作得很好,但是只要我按 float value (进行测试),输出屏幕就会进入无限循环,调试器甚至不会停在 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

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

相关问题