2016-04-30 11 views
0

私はキューを含むレッスンに取り組んでいます。キューのクラステンプレートのenqueueとdequeue(クラステンプレートの両方のブール関数)のコンセプトに固執しています。ファイル内の数字などのユーザー入力ではない可能性があります。Forループを使用してキューをエンキューしてデキューする - C++

for(int i = 1; i <= 10; i++) 
{ 
    if(i % 2 == 0) 
     while(intQueue.enqueue(i)) 
      cout << i << " has been added to the queue . . .\n"; 
} 

しかし、私はいくつかの理由:私は例が唯一のキューに(リスト1-10から)も、整数を配置するため、待ち行列に特定の項目をキューに入れるために、forループを実行してみました唯一の繰り返しでキューに追加された最初のアイテム取得:私が正しく何かをやっていなかった場合、私は思っていた

2 has been added to the queue . . . 
2 has been added to the queue . . . 
2 has been added to the queue . . . 
2 has been added to the queue . . . 
2 has been added to the queue . . . 

を、または特定のアイテムをエンキューするために、さまざまな方法があるかどう。どんな助けやヒントも大歓迎です。

+2

「while()」を削除してください – simpel01

答えて

2

while(intQueue.enqueue(i))intQueue.enqueue(i))trueと評価される値を返す限り、引き続き実行されます。

あなたが使用する必要があるのはifです。

for(int i = 1; i <= 10; i++) 
{ 
    if (i % 2 == 0) 
    { 
     if (intQueue.enqueue(i)) 
      cout << i << " has been added to the queue . . .\n"; 
     else 
      cout << i << " has not been added to the queue . . .\n"; 
    } 
} 
+0

これは機能しました!ご協力いただきありがとうございます! –

+0

@JosiahHolmes、それを聞いてうれしい。 –

+0

@JosiahHolmes - それが助けられたら、答えを受け入れるべきです – 4386427

関連する問題