2011-08-04 5 views
2

あなたはすべて過去に私を助けてくれました。ユーザーにアレイへの入力を求めるループ

私は学校の別のC++割り当てに取り組んでいます。私はユーザーから10と100の間の20の数字を集め、それらをすべて比較し、重複していないものを印刷しなければならない。

私が立ち往生している部分は、20個の数字がすべて入るまでループするユーザー入力を得ようとしています。私が持っているものは私には意味をなさないが、それは明らかに機能していない。ばかげたコメントを申し訳ありません、私はクラスのためにすべてをコメントする必要があります。

あなたは私のばかに上げることができる光は素晴らしいでしょう!どうもありがとうございます!

int FindDuplicates[20]; // my little array 
int UserInput; // the number from the user to compare 
int count; // the number of numbers entered 

for(count=0; count<FindDuplicates; count++;) // what I am trying to do is have count start at 0. as long as it is less than the max array, add 1 to the count 
{ 
    cout << "Please enter a number between 10 and 100: "; // print out what I would like 
    cin >> UserInput; // get the number from the user 
} 

答えて

3

あなたの問題は20の有効数字を入力する方法ですか? do whileループはあなたを助けると思います。

int FindDuplicates[20]; // my little array 
int UserInput; // the number from the user to compare 
int count = 0; // the number of numbers entered 

do 
{ 
    cout << "Please enter a number between 10 and 100: "; // print out what I would like 
    cin >> UserInput; // get the number from the user 

    if (UserInput >= 10 && UserInput <= 100) 
    count++; 
} while (count < 20); 

希望はあなたを助けます。

+0

ありがとうございました!それは今私がそれを見ることはとても簡単です!私が追加しなければならなかったのは、Countでマークされたスロットの配列にUserInputを追加した行でした! – audiedoggie

1

FindDuplicatesポインタが配列(thisを参照)ではなく、サイズが指すあなたを与えるだろう。代わりに

for(count=0; count<20; count++;)

を使用する必要があります。

1

以下の行に構文エラーがあります。どのように動作するかを確認してください。 <FindDuplicatesにも問題があります。その変数の意味を理解してください。

for(count=0; count<FindDuplicates; count++;)

3
for(int count = 0; count < 20; count++) 
{ 
    cout << "Please enter a number between 10 and 100: "; 
    cin >> userInput; 

    FindDuplicates[ count ] = userInput; 
} 

またして有効な入力を確認することができます:あなたはちょうどあなたが何かに変数UserInputを初期化していることを確認してくださいことを行う場合

while(UserInput < 10 || UserInput > 100) 
{ 
    cout << "Please enter a number between 10 and 100: "; 
    cin >> UserInput; 
} 

。だから:

int FindDuplicates[ 20 ]; 
int UserInput = 0; 
int count = 0; 

//count has already been defined, so no need to do it again 
for(; count < 20; count++) 
{ 
    //Keep asking until valid input is given 
    while(UserInput < 10 || UserInput > 100) 
    { 
     cout << "Please enter a number between 10 and 100: "; 
     cin >> userInput; 
    } 

    //Add userInput into the array 
    FindDuplicates[ count ] = UserInput; 
} 
関連する問題