0

I have been asked to write a lottery program in c++ not sure if i am using function correctly please help

//function prototypes
int myNumbers();
void displayNums();

#include <stdio.h>
#define NUMS 6
#define WIN 7


// function myNumbers takes input from user 
int myNumbers(int numbers[]) //implememt function 
{
    int i;
    int numbers;
    int input[NUMS];
    printf ("Please enter your lucky numbers\n");

    for (i=0;i<NUMS;i++)//loop through array to take input
    {
        scanf("%d",&input[i]);                
    }//end for loop


    return (numbers);        
}//end function myNumbers
3
  • you are returning only one int from your myNumber and not using passed userPick[]. Why? Commented Mar 19, 2014 at 15:45
  • What are you trying to achieve here: numbers = *(input+i);? Commented Mar 19, 2014 at 15:49
  • 1
    .. and this one int is even picked out of the bounds of the array. @zoska Commented Mar 19, 2014 at 15:52

4 Answers 4

1

Each of your functions declares a new array, rather than using the array passed to it as an argument.

void displayNums(int print[])
{
    int i;
    int output;
    int numbers[NUMS];
    output = myNumbers(numbers);
    printf("Your numbers are %d \n", output);
}

Note that nowhere is the print argument used, and you're using int numbers[NUMS] instead. Remove that declaration and use print. (Also please consider naming your argument something other than print; this name is confusing and does not accurately describe what the variable stores.)

Sign up to request clarification or add additional context in comments.

Comments

1

you are not using arrays properly to communicate the numbers, see the function

int myNumbers(int userPick[]) //implememt function 
{
    int i;
    int numbers;
    int input[NUMS];
    printf ("Please enter your lucky numbers\n");

    for (i=0;i<NUMS;i++)//loop through array to take input
    {
        scanf("%d",&input[i]);                
    }//end for loop
    numbers = *(input+i);

    return (numbers);        
}//end function myNumbers

it reads the number to a local array and returns *(input+i) which will be a random number since your read array is from input+0 to input+i-1. you should pass array or pointer to global array.

Even in case of display() function you are passing one array and using some other array inside display() function.
you should use a common array to communicate values. you can create a array in global scope and use it in all functions or create a array in main() and pass pointer to it to other functions and use the same array in other functions. learn how to pass arrays between functions and use arrays

Comments

0

Your functions looks erroneous:

  1. int myNumbers(int userPick[]) // 1. userPick is not used anywhere
    {
    
    int i;
    int numbers;               // 2. initialize this variable to 1 first 
    int input[NUMS];
    printf ("Please enter your lucky numbers\n");
    
    for (i=0;i<NUMS;i++)
    {
        scanf("%d",&input[i]);                
    }//end for loop
    numbers = *(input+i);     // 3. is this suppose to be inside the loop ?
    
    return (numbers);
    }//end function myNumbers
    

And in

void displayNums(int print[])//implement function
{
    int i;
    int output;
    int numbers[NUMS];
    output = myNumbers(numbers);
    printf("Your numbers are %d \n",output);
}//end function displayNums

You don't use print but create a new array numbers

Comments

0

Most of your code is erroneous. It won't compile. Here's some sample code. Work with it to implement the rest

#include <stdio.h>
#define NUMS 6

void displayNums(int nums[])
{
    int i;
    printf("Your numbers are:\n");
    for(i = 0; i < NUMS; i++)
        printf("%d ", nums[i]);
    printf("\n");
}

void myNumbers(int nums[])
{
    printf("Please Enter your lucky numbers\n");
    int i;
    for(i = 0; i < NUMS; i++)
        scanf("%d", &nums[i]);
}

int main()
{
    int numbers[6];
    myNumbers(numbers);
    displaynums(numbers);

    //do the rest of the stuff here

    return 0;
}

Going through a C tutorial might be helpful? Check out http://c.learncodethehardway.org/book/ for a nice intro to the language.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.