简体   繁体   中英

Argument of type “int” is incompatible with parameter of type “int *”

The task

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.

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. 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.

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.

Once that is fixed, you then have the problem of genNumber returning an array defined locally. 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 .

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM