任务
Design a program that generates a 7-digit lottery number. The program should have an INTEGER array with 7 elements. Write a loop that steps through the array, randomly generating a number in the range of 0 to 9 for each element.Then write another loop that displays the contents of the array.
这是我的代码
#include <iostream>
#include <cstdlib>
#include <array>
using namespace std;
// Global Constants
const int ARRAY_SIZE = 7;
// Declare Functions
int genNumber();
void displayResults(int lottoArray[], int ARRAY_SIZE);
// Module -- Main
int main(){
// Declare Variables
int LottoArray;
// Set Functions
LottoArray = genNumber();
// Call Display Function
displayResults(LottoArray, ARRAY_SIZE);
system("pause");
return 0;
}
// Module -- Generate Numbers
int genNumber(){
// Declare Variables for Array
int lottoArray[ARRAY_SIZE];
int i = 0;
// Generate a Number for Each Array Placeholder
for (i = 0; i < ARRAY_SIZE; i++){
// Generate Random Number in Array
lottoArray[i] = rand() % 10;
int checkNumber = lottoArray[i];
// Check for multiples
for (int i : lottoArray){
while (i == checkNumber){
checkNumber = rand() % 10;
}
}
}
return lottoArray[ARRAY_SIZE];
}
// Module -- Display Results
void displayResults(int lottoArray[], int ARRAY_SIZE){
// Declare Variables
int i = 0;
cout << "Lotto Numbers: ";
// Display Each Value in the Array
for (i = 0; i < ARRAY_SIZE; i++){
cout << lottoArray[i] << " ";
}
cout << endl;
}
我在这部分代码的标题上遇到错误
// Call Display Function
displayResults(LottoArray, ARRAY_SIZE);
我知道问题是我没有返回单个 int,而是返回了多个。我将如何解决这个问题?我的程序必须完全模块化,主函数只能用来调用其他函数。
最佳答案
有几个问题。
genNumber
需要返回一个 int *
,而不是 int
,并且 main 中的
也需要定义为 LottoArray
int *
。
一旦该问题得到解决,您就会遇到 genNumber
返回本地定义的数组的问题。函数返回后,该数据将变为未定义。
因为您知道数组的起始大小,您最好将 LottoArray
(及其大小)传递给 genNumber
并直接写入它,并将返回类型更改为 void
。
所以你现在有以下内容:
#include <iostream>
#include <cstdlib>
#include <array>
using namespace std;
// Global Constants
const int ARRAY_SIZE = 7;
// Declare Functions
void genNumber(int lottoArray[], int size);
void displayResults(int lottoArray[], int size); // don't mask global variables
// Module -- Main
int main(){
// Declare Variables
int LottoArray[ARRAY_SIZE]; // define an array here
// Set Functions
genNumber(LottoArray, ARRAY_SIZE);
// Call Display Function
displayResults(LottoArray, ARRAY_SIZE);
system("pause");
return 0;
}
// Module -- Generate Numbers
void genNumber(int lottoArray[], int size){
// Declare Variables for Array
int i = 0;
// Generate a Number for Each Array Placeholder
for (i = 0; i < size; i++){
// Generate Random Number in Array
lottoArray[i] = rand() % 10;
int checkNumber = lottoArray[i];
// Check for multiples
for (int i : lottoArray){
while (i == checkNumber){
checkNumber = rand() % 10;
}
}
}
}
// Module -- Display Results
void displayResults(int lottoArray[], int size){
// Declare Variables
int i = 0;
cout << "Lotto Numbers: ";
// Display Each Value in the Array
for (i = 0; i < size; i++){
cout << lottoArray[i] << " ";
}
cout << endl;
}
关于c++ - 类型 "int"的参数与类型 "int *"的参数不兼容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33424188/