[英]Argument of type “int” is incompatible with parameter of type “int *”
The task 任务
Design a program that generates a 7-digit lottery number.
设计一个生成7位彩票号码的程序。 The program should have an INTEGER array with 7 elements.
该程序应具有7个元素的INTEGER数组。 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.
编写一个循环遍历数组的循环,为每个元素随机生成一个介于0到9之间的数字,然后编写另一个循环以显示数组的内容。
This is my code 这是我的代码
#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;
}
I get the error on the title on this part of the code 我在代码的这一部分出现标题错误
// Call Display Function
displayResults(LottoArray, ARRAY_SIZE);
I understand the problem is that I'm not returning a single int, I'm returning multiple. 我知道问题是我没有返回单个int,而是返回了多个。 How would I go about fixing this?
我将如何解决这个问题? My program has to be completely modular, and the main function can only be used to call the other functions.
我的程序必须是完全模块化的,并且main函数只能用于调用其他函数。
There are several issues. 有几个问题。
genNumber
needs to return an int *
, not an int
, and LottoArray
in main
needs to be defined as int *
as well. genNumber
需要返回int *
,而不是int
和LottoArray
在main
需要被定义为int *
为好。
Once that is fixed, you then have the problem of genNumber
returning an array defined locally. 解决此问题后,您将
genNumber
返回本地定义的数组的问题。 Once the function returns, that data becomes undefined. 函数返回后,该数据将变得不确定。
Since you know how big the array is to begin with, you're probably better off passing LottoArray
(and its size) to genNumber
and writing to it directly, and changing the return type to void
. 由于您知道数组的起始大小,
genNumber
最好将LottoArray
(及其大小)传递给genNumber
并直接genNumber
进行写入,然后将返回类型更改为void
。
So you now have the following: 因此,您现在具有以下内容:
#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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.