Lottery application assignment

This is a discussion on Lottery application assignment within the C++ Programming forums, part of the General Programming Boards category; Hi everyone, I am having a problem to write a program that simulates a lottery. I hope someone can guide ...

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    15

    Lottery application assignment

    Hi everyone, I am having a problem to write a program that simulates a lottery. I hope someone can guide me to fix the problem/error step by step. Thank you.

    Here is the question: The program should have an array of five integers named lottery, and should generate a random number in the range of 0 to 9 for each element in the array.

    The user should enter five digits which should be stored in an integer array named user.

    The program is to compare the corresponding elements in the two arrays and keep a count of the digits that match. For example, the following shows the lottery array and the user array with sample numbers stored in each. There are two matching digits (elements 2 and 4).


    lottery array: 7, 4, 9, 1, 3


    user array: 4, 2, 9, 7, 3


    The program should display the random numbers stored in the lottery array and the number of matching digits. If all of the digits match, display a message proclaiming the user as a grand prize winner.



    Your program must use the following functions:


    void generateNumbers(int [ ], int);

    void getUser(int [ ], int);

    int findMatches(int [ ], int [ ], int);

    void displayValues(int [ ], int [ ], int);

    And here is my code:
    Code:
    .
    // Chapter 7
    // Assignment 7-Lottery Application.
    #include <iostream>
    #include <time.h>
    using namespace std;
    
    
    // Function prototype
    void generateNumbers(int[], int);
    void getUser(int[], int);
    int findMatches(int[], int[], int);
    void displayValues(int[], int[], int);
    
    
    int main()
    {
    	const int SIZE = 5;			// Size of an array.
    	int lotteryArray[SIZE];		// To hold the lottery's number in randomly. 
    	int userArray[SIZE];		// To hold the user's number. 
    	
    	// Function begin
    	cout << "This program can helps you win a lottery game." << endl;
    	
    	// Get the number from lottery array.
    	generateNumbers(lotteryArray, SIZE);
    	
    	// Get the number from user.
    	getUser(userArray, SIZE);
    
    
    	// Find and match the lottery and user array.
    	findMatches(lotteryArray, userArray, SIZE);
    
    
    	// Display a message.
    	displayValues(lotteryArray, userArray, SIZE);
    
    
    	
    
    
    	system ("PAUSE");
    	return 0;
    }
    
    
    //*************************************************
    // The message displays a generateNumbe function. *
    // ************************************************
    
    
    void generateNumbers(int lotteryArray[], int SIZE)
    {
    	srand(time(0));
    	int randNum = rand() % 9+0;	// Each elements in the array that genertate a randNum in the range of 0 - 9.
    
    
    
    
    	randNum = lotteryArray[0];
    
    
    	cout << "The lottery program will generate some random numbers "
    		 << "in the range of 0 to 9";
    	cout << "Here is " << SIZE << "element in the array";
    	cin >> randNum;
    
    
    	return 0;
    }
    
    
    //**********************************************
    // This message displays a getUser function.   *
    //**********************************************
    
    
    void getUser(int userArray[], int SIZE)
    {
    	// Loop counter
    	int index;
    
    
    	for(index = 0; index < SIZE; index++)
    	{
    		cout << "Enter " << SIZE << "digits number to win the lottery: ";
    		cin >> userArray[index];
    	}
    }
    
    
    //**************************************************
    // This message displays a findmatches function.   *
    //**************************************************
    
    
    int findmatches(int lotteryArray[], int userArray[], int SIZE)
    {
    	bool arraysEqual = true;	// Flag variable.
    	int count = 0;				// Loop counter varabile.
    
    
    	// Determine whether the elements contain the same data.
    	while (arraysEqual && count < SIZE)
    	{
    		if (lotteryArray[count] != userArray[count])
    			arraysEqual = false;
    		count++;
    	}
    	if (arraysEqual)
    		cout << "The arrays are match.\n";
    	else 
    		cout << "The arrays are not match.\n";
    	
    	return 0;
    }
    
    
    //**************************************************
    // This message displays a displayValues function. *
    //**************************************************
    
    
    void displayValues(int lotteryArray[] , int userArray[], int SIZE)
    { 
    	int arraysEqual;
    	
    	if(arraysEqual = false)
    		cout << "Sorry your numbers weren't match to the wining number, please try that again.\n";
    	else 
    		cout << "Congratulations, you are a grand prize winner.\n";
    
    
    }
    .

    Here are the problems that I got:
    2010\projects\improve\improve\improve.cpp(46): warning C4244: 'argument' : conversion from 'time_t' to 'unsigned int', possible loss of data
    2010\projects\improve\improve\improve.cpp(57): error C2562: 'generateNumbers' : 'void' function returning a value
    2010\projects\improve\improve\improve.cpp(8) : see declaration of 'generateNumbers'
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,663
    > srand(time(0));
    Try
    srand((unsigned int)time(0));

    > 2010\projects\improve\improve\improve.cpp(57): error C2562: 'generateNumbers' : 'void' function returning a value
    Can you see it?
    Code:
    void generateNumbers(int lotteryArray[], int SIZE)
    {
        srand(time(0));
        int randNum = rand() % 9+0; // Each elements in the array that genertate a randNum in the range of 0 - 9.
     
     
     
     
        randNum = lotteryArray[0];
     
     
        cout << "The lottery program will generate some random numbers "
             << "in the range of 0 to 9";
        cout << "Here is " << SIZE << "element in the array";
        cin >> randNum;
     
     
        return 0;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,777
    There are a lot of problems in this code. First, have a read at SourceForge.net: Do not remove parameter names - cpwiki. Though you've been given prototypes to use, presumably there is no harm in adding parameter names to them.

    randNum = lotteryArray[0]; in generateNumberd is probably backwards. You are assigning the 0th element of lotteryArray to randNum, which makes no sense. Besides, that element's value is undefined at this point. I suggest you go to project settings -> C/C++ -> General and changing warning levels to 4.
    Also, I assume you should generate 5 random numbers, not one.
    Later, what you output to screen makes no sense with what the line of code is doing. What are you supposed to do, really?

    In getUser, change
    for(index = 0; index < SIZE; index++)
    to
    for(int index = 0; index < SIZE; index++)
    and remove the int index line above. Prefer declaring variables near use. You can even define them inside loops.

    displayValues is broken.
    Firstly you define a new arraysEqual variable whose value is undefined (you never initialize it!). Secondly, you give it type int while clearly it is a bool. And finally, you try to check if the arrays for the user and the lottery are equal by testing this. Obviously it cannot possibly say whether they are equal since it's a variable you just defined!
    To make matters worse, you've confused = for == (ie assignment for comparison).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,438
    It might not be a rule that is required for your assignment - but your lottery numbers could all be the same or contain lots of repeats at the moment, as you have not written anything to try and stop that happening.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  5. #5
    Registered User
    Join Date
    Jul 2012
    Posts
    15
    Hello everyone, thanks for all your response. This actually is a draft code, so there are loads of error need to fix.

  6. #6
    Registered User
    Join Date
    Jul 2012
    Posts
    15
    can you guide me how to fix those error? I know there are some repeat numbers and moments.

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,777
    Be specific about what you do not understand.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Jul 2012
    Posts
    15
    I can't even build the random number in generateNumbers function. I am confusing how to input the random number in the generateNumbers function.
    Code:
    .
    void generateNumbers(int lotteryArray[], int SIZE)
    {
           srand((unsigned int)time(0));
           int randNum = rand() % 9+0; // Each elements in the array that genertate a randNum in the range of 0 - 9.
     
           randNum = lotteryArray[5];
     
           cout << "The lottery program will generate some random numbers "
                   << "in the range of 0 to 9." << endl;
           cout << "Here are " << SIZE << " numbers in the array";
           cout << randNum << endl;
     
    }
    .

    For example, I want the program can build some random numbers after it compilers. Like the instruction question says, "should generate a random number in the range of 0 to 9 for each element in the array." I am wondering did I do it in the right way, or there is another way to do it. Thank you.

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,777
    Like I already mentioned, you have assignment backwards. You assign lotteryArray[5] to randNum, no the other way around.
    Your random number generation is wrong, too. The principle is right, but you should use "% 10" to generate between 0 - 9.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    Jul 2012
    Posts
    15
    Hmh, I think I got it. I just finish the program and it runs perfectly(no error). Thanks man

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,777
    I think, that to be sure you did the right thing, you should post your most up-to-date code.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Registered User
    Join Date
    Jul 2012
    Posts
    15
    Here is the up-to-date code:
    Code:
    .
    // Chapter 7
    // Assignment 7-Lottery Application.
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    using namespace std;
     
    const int ARRAY_SIZE = 5;           // SIZE of an array.
    const int MAX_RANGE = 9;            // The max range between 0 to 9.
     
    // Function prototype
    void generateNumbers(int[], int);
    void getUser(int[], int);
    int findMatches(int[], int[], int);
    void displayValues(int[], int[], int);
     
    int main()
    {
           int lottery[ARRAY_SIZE];            // To hold the lottery's number in randomly. 
           int user[ARRAY_SIZE];               // To hold the user's number.
           int numMatches;                                   // To holf the matching number.
          
           // Function begin
           cout << "This program can helps you win a lottery game." << endl;
          
           // Get the number from lottery array.
           generateNumbers(lottery, ARRAY_SIZE);
          
           // Get the number from user.
           getUser(user, ARRAY_SIZE);
     
           // Find and match the lottery and user array.
           numMatches = findMatches(lottery, user, ARRAY_SIZE);
     
           // Display a message.
           displayValues(lottery, user, ARRAY_SIZE);
     
           cout << "Your number matched " << numMatches << " numbers.\n";
           if (numMatches == 5)
           { 
                  cout << "congratulations, you are a grand prize winner.\n";
           }
           else
           {
                  cout << "Sorry, be better luck next time.\n";
           }
     
           system ("PAUSE");
           return 0;
    }
     
    //*************************************************
    // The message displays a generateNumbe function. *
    // ************************************************
     
    void generateNumbers(int lottery[], int ARRAY_SIZE)
    {
           srand((unsigned int)time(0));
     
           cout << "The lottery program will generate some random numbers "
                   << "in the range of 0 to 9." << endl;
           for (int index = 0; index < ARRAY_SIZE; index++)
           {
                  lottery[index] = 0 + rand() % MAX_RANGE;
           }
           cout << endl;
    }
     
    //**********************************************
    // This message displays a getUser function.   *
    //**********************************************
     
    void getUser(int user[], int ARRAY_SIZE)
    {
     
           for(int index = 0; index < ARRAY_SIZE; index++)
           {
                  cout << "Enter a number " << (index + 1) << ": "; 
                  cin >> user[index];
           }
    }
     
    //**************************************************
    // This message displays a findmatches function.   *
    //**************************************************
     
    int findMatches(int lottery[], int user[], int ARRAY_SIZE)
    {
           bool arraysEqual = true;     // Flag variable.
           int count = 0;                      // Loop counter.
           int numMatches = 0;
     
           // Determine whether the elements contain the same data.
           while (arraysEqual && count < ARRAY_SIZE)
           {
                  if (lottery[count] != user[count])
                          arraysEqual = false;
                  count++;
           }
           if (arraysEqual)
                  cout << "The arrays are match.\n";
           else 
                  cout << "The arrays are not match.\n";
          
           return numMatches;
    }
     
    //**************************************************
    // This message displays a displayValues function. *
    //**************************************************
     
    void displayValues(int lottery[] , int user[], int ARRAY_SIZE)
    { 
           for (int i = 0; i < ARRAY_SIZE; i++)
           {
                  cout << lottery[i] << " ";
           }
           cout << endl;
           for (int u = 0; u < ARRAY_SIZE; u++)
           {
                  cout << user[u] << " ";
           }
           cout << endl;
    }
    .

  13. #13
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,777
    Code:
    void generateNumbers(int[], int);
    void getUser(int[], int);
    int findMatches(int[], int[], int);
    void displayValues(int[], int[], int);
    This still isn't fixed.
    SourceForge.net: Do not remove parameter names - cpwiki

    You should change
    srand((unsigned int)time(0));
    to
    srand((unsigned int)time(nullptr));
    unless your compiler complains (nullptr is the new "null" for pointers in C++; 0 is the "old null").
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Registered User
    Join Date
    Jul 2012
    Posts
    15
    Okay, I fixed it. Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can someone help me with my C programming lottery code?
    By OvdoMan9 in forum C Programming
    Replies: 2
    Last Post: 09-02-2010, 11:07 PM
  2. help with a lottery problem.
    By todouble22 in forum C++ Programming
    Replies: 17
    Last Post: 01-26-2010, 11:57 AM
  3. Lottery game
    By got1sleeve in forum C++ Programming
    Replies: 3
    Last Post: 02-16-2008, 12:55 PM
  4. Lottery game
    By geetard in forum C++ Programming
    Replies: 2
    Last Post: 12-19-2005, 11:50 PM
  5. Yet another lottery program
    By BungleSpice in forum C Programming
    Replies: 10
    Last Post: 04-01-2004, 01:08 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21