2016-11-15 12 views
0

私は宿題のために書いているプログラムのために機能が正しく働きません。この割り当てでは、ユーザーが1〜40の7つの数字を推測する宝くじシミュレーションを作成するように求められます。その数字は、別の関数からランダムに生成された数字と比較されます。この関数はを頼むと、アレイ内の7つの番号を格納するためのものです:数字を配列に入力する

const int size = 7; 

int getLottoPicks(int userNum[size]) { //collects and stores the user input 

for (int i = 0; i < size; i++) { 
    cout << "Please enter number " << i+1 << ": "; 
    cin >> userNum[i]; 

if (userNum[i] < 1 || userNum[i] > 40) { //keeps the number between 1 and 40 
    cout << "The number must between 1 and 40." << endl 
     << "Please enter another number: "; 
    cin >> userNum[i]; 
} 
} 

return userNum[size]; 
} 

現在、この機能ではなく、入力された数字の0096F71Cのようなクレイジーなものを出力します。

呼び出されたときに7数値配列を出力するには、どのような変更が必要ですか? また、重複する値が見つからないようにするにはどうすればよいでしょうか?

ありがとうございます。

答えて

0

この機能では、プロンプト以外は出力されません。そして、配列の最後の1つ前の要素を返します。ここで未定義の動作が起こっています。

あなたの関数が与えられた配列に関数を既に挿入しているので、何も返す必要はないとおもいます。これで修正できます。

const int size = 7; 

void getLottoPicks(int userNum[size]) { //collects and stores the user input 

    for (int i = 0; i < size; i++) { 
    cout << "Please enter number " << i+1 << ": "; 
     cin >> userNum[i]; 

    if (userNum[i] < 1 || userNum[i] > 40) { 
     cout << "The number must between 1 and 40." << endl 
      << "Please enter another number: "; 
     cin >> userNum[i]; 
    } 

    for (int j = i; j > 0; --j) { 
     if (userNum[i] == userNum[j]) { 
     cout << "Already entered this number"; 
     } 
    } 
    } 
} 
+0

私は助けていただきありがとうございます。しかし、私のプログラムでこれらの変更を加えたとき、関数を呼び出すときにエラーが発生しました。私はそれを呼び出す: _userTicket [size] = getLottoPicks(userTicket); _ と私が受け取ったエラーは "_" '=': 'void'から 'int'に変換できません "。 –

+0

@MaxOrozco、そうです。あなたの配列 'int userTicket [size];を宣言してから別の行で関数を呼び出してください。' getLottoPicks(userTicket); ' – StoryTeller

関連する問題