C++ pass array help

I need it to return the entire array from userNumbers function and store it in usernum array in main function.

**there are some blank functions right now because i didnt code there purpose yet, i need to figure out how to pass array for most of assignment**


Here is my code so far

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
//****************************************************************************
// Programmer: 
//
// Description:
//*****************************************************************************
#include <iostream>
#include <fstream>
#include <iomanip>

void	pickNumbers(int[]);
int	userNumbers(int[]);
void	checkNumbers();
void	printWinnings();
 
using namespace std;

int main (){  
	int usernum [4];
	for (int i=0; i<5; i++){
		usernum [i]=userNumbers();// <==help here
	}
	
	int winnum [4];
	cout << usernum[0]<< usernum[1] <<usernum[2]<<usernum[3]<<usernum[4];
	return 0;
}

int userNumbers(int num1[]){
	cout << "Please enter your lottery numbers seperated by spaces";
	cin >> num1[0],num1[1],num1[2],num1[3],num1[4];
	for (int i=0; i<5; i++){
	return num1[i];
	}}

void pickNumbers(int num[]){


}

void checkNumbers(){


}

void printWinnings(){


}

Last edited on
1. You can't "return" a basic array. Arrays are evil and should be avoided at all costs, but I'm guessing you are required to use them for this. Instead of returning the array, pass the array as a pointer so the function can modify the existing array.

2. Your array has four elements, 0 to 3, but you are treating it like it has 5 elements, which can cause segfaults if you're lucky.
use pointers like L B said, or use a vector.
closed account (zb0S216C)
If you know the length of the array, pass the array by reference. Here's an example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
namespace Constants 
{
    unsigned int const ARRAY_SIZE( 4u );
}

void Something( int( &Array_ )[Constants::ARRAY_SIZE] )
{
    Array_[...] = ...;
}

int main( )
{
    int Array_[Constants::ARRAY_SIZE] = { 0 };
    ::Something( Array_ );
}

Wazzak
That line 14 is to die four.
There will be 2 arrays, currently trying to get first one to work. each array will have 5 numbers.
when you say

int winnum [4];

does that mean there is 5 memory allocations 0-4 or is it total number of spots you need to reserve.

When you pass an array by default, does it make it editable by all functions, or temporarily editable until function is over?
closed account (zb0S216C)
jkfde wrote:
"That line 14 is to die four."

I'm explicit with scoping, even if it's obvious.

jkfde wrote:
"When you pass an array by default"

What do you mean by that?

Wazzak
I meant it passes an array by reference as default, oops.
jkfde wrote:
when you say

int winnum [4];

does that mean there is 5 memory allocations 0-4 or is it total number of spots you need to reserve.
That means you have an array of 4 integers,with indices 0 to 3.
Last edited on
Topic archived. No new replies allowed.