2016-03-21 9 views
0

次のコードは、可能な限り単純化されており、は、以下を実行するを想定している。クロスチェック整数

1)ユーザが5つの整数のリストを移入します。

2)ユーザーは「番号」を入力します。

3A)「数」は5つの整数のものと同じである場合 - 二重その数は「数」は5のものと同じでない場合2.

3B)を繰り返し、その後、表示されます整数 - 終了プログラム。

実行時にリストにある番号を入力し、リストにない出力応答を取得します。また、このelseステートメントをアクティブにすると、whileループはa = 0として停止すると予想されますが、ループは継続します。私は数に関するエラー嘘がリストに表示されていないと思うのはここ

は次のとおりです。

if (number == list[x]) 

私は括弧内の間違った文を使用している場合があります。

しかし、私はそれがなぜまたループし続けているのか分かりません。おそらく両方のエラーは同じ事柄のためです。上記のようにプログラムを実行するための助けがあれば幸いです。ここで

は完全なコードです:

#include <iostream> 
using namespace std; 
int main() 
{ 
int list[5]; 
int a, x, number; 
a = 1; 
cout << "Fill up the list" << endl; 
for (x = 0; x <= 4; x++){ 
    cin >> list[x]; 
    } 

while (a = 1) { 

cout << "enter one of the numbers you put on the list" << endl; 
cout << "we will double it " << endl; 
cin >> number ; 
if (number == list[x]) 
{ 
    cout << "double that number is " << number * 2 << endl; 
} 
else 
{ 
    cout << "Thats not on the list" << endl; 
    a = 0; 
} 
} 

return 0; 
} 
+0

ヒント:xの値は何ですか? –

+0

私は推測x = 4右ですか? –

答えて

0

私は

if ((number == list[0])||(number == list[1])|| (number == list[2]) || (number == list[3])||(number == list[4])) 

if (number == list [x]) 

を変更し、

while (a = 1) 
を変更しました誰が代わりに提案を開い

if ((number == list[0])||(number == list[1])|| (number == list[2]) || (number == list[3])||(number == list[4])) 

イムの選択肢を知っているしかし、場合

while (a == 1) 

から

完全なコードは以下のようになります。

#include <iostream> 
using namespace std; 
int main() 
{ 
int list[5]; 
int a, x, number; 
a = 1; 
cout << "Fill up list bitch" << endl; 
for (x = 0; x <= 4; x++){ 
    cin >> list[x]; 
    } 

while (a == 1) { 

cout << "enter one of the numbers you put on the list" << endl; 
cout << "we will double it " << endl; 
cin >> number ; 
if ((number == list[0])||(number == list[1])|| (number == list[2]) || (number == list[3])||(number == list[4])) 
{ 
    cout << "double that number is " << number * 2 << endl; 
} 
else 
{ 
    cout << "Thats not on the list you scumbag" << endl; 
    a = 0; 
} 
} 

return 0;