小编Amo*_*del的帖子

卡在无限循环中

在C++中,为了处理错误的输入(比如当程序要求输入一个整数但你输入一个字符时)它应该能够做某事然后循环重复输入.

当需要整数时输入字符时,我的循环无限迭代,反之亦然:

#include<iostream>

int main()
{
    while (cout << "Enter a number" && !(cin >> num)) 
    {
        cin.sync(); 
        cin.clear();
        cout << "Invalid input; please re-enter.\n";
    }
}
Run Code Online (Sandbox Code Playgroud)

我应该怎么做才能让程序再次提示输入新内容?

c++ int iostream infinite-loop while-loop

4
推荐指数
1
解决办法
1114
查看次数

无法访问功能

我为下面给出了二进制堆的程序 -

       #include<iostream>
     using namespace std;
        /**
         * Construct the binary heap.
         * capacity is the capacity of the binary heap.
         */

class BinaryHeap
        {

           private:
            int   currentSize;  // Number of elements in heap
            int array[];        // The heap array

            void buildHeap( );
            void percolateDown( int hole );
           public:
            bool isEmpty( ) const;
            bool isFull( ) const;
            int findmini( ) const;

            void insert( int x );
            void deleteMin( );
            void deleteMin( int minItem );
            void makeEmpty( );



       public :
       BinaryHeap( …
Run Code Online (Sandbox Code Playgroud)

c++ oop class object

0
推荐指数
1
解决办法
68
查看次数

标签 统计

c++ ×2

class ×1

infinite-loop ×1

int ×1

iostream ×1

object ×1

oop ×1

while-loop ×1