Like Tree5Likes
  • 2 Post By Matticus
  • 2 Post By rags_to_riches
  • 1 Post By Shakti

I need HELP!!!

This is a discussion on I need HELP!!! within the C++ Programming forums, part of the General Programming Boards category; Write a program that simulates a lottery. The program should have an array of five integers named lottery, and should ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    3

    I need HELP!!!

    Write a program that simulates a lottery. The program should have an array of five integers
    named lottery, and should generate a random number in the range of 0 through 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 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 of $25,000. If there are four matching digits, the prize should be $1,000, and if
    there are three matching digits, the prize should be $500.

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    2,287
    Step 1: Read the sites homework policy to understand why no one will give you a complete solution.
    Step 2: Post your attempt, being sure to use code tags, and we will assist you with any specific problems you have.

    Oh, and welcome to the forums.
    Salem and Ameone like this.

  3. #3
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,650
    Name:  12200d1352722106-how-can-do-cplusplus-tumbleweed_.gif
Views: 181
Size:  16.9 KB
    Salem and Ameone like this.

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    3
    Write a program that simulates a lottery. The program should have an array of five integers
    named lottery, and should generate a random number in the range of 0 through 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 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 of $25,000. If there are four matching digits, the prize should be $1,000, and if
    there are three matching digits, the prize should be $500.


    I made it and I have problems to make it match 4 digits and 3 also.

    My work is:

    Code:
     
    Code:
    #include <iostream>
    #include <cstdlib>
    #include <iomanip>
    #include <ctime>
    using namespace std;
    
    
    
    
    int main ()
    
    
    {  
    
    
        const int SIZE = 5;         //Size of the arrays.
              int user[SIZE];       //To hold the user's lucky numbers.
              int lottery[SIZE];        //To hold the random numbers.
              int count = 0;        //Loop counter.
              bool numMatch = true;          //Flag variable.
        
    
    
            cout << "Do you feel lucky today?  If so, enter your " <<SIZE<<" lucky numbers \n";
            cout << "between 0 and 9: \n";
    
    
            //Get five numbers from the user and store them in the user array.   
    
    
            for (count = 0; count < SIZE; count++)
    
    
            {
                cout << "Please enter lucky number # " <<(count+1)<<":";
                cin  >> user[count];
            }    
            //Generate five random numbers and store them in the lottery array.
    
    
            srand((unsigned int)time(0));
    
    
            for (count = 0; count < SIZE; count++)
            {
                lottery[count] = 1 + rand()%9;
            }
            //Display the contents of lottery array.
            cout << "The winning numbers are:  \n";
            
            for (count =0; count < SIZE; count++)
            {
                cout <<" " <<lottery[count];
                cout << endl;
            }
     
            //Determine whether there are matching numbers.
    
    
            for (count =0; count < SIZE; count++)
            {
               while (numMatch && count < SIZE)
                {
                    if (user[count] != lottery [count])
                        numMatch = false;
                    count++;
                }
            }
    
    
            if (numMatch)
                cout << "Congratulations you are the Grand Prize Winner!!!\n";
            else if (numMatch = 4)
                cout << "Congratulations you win 1,000!!!\n";
            else if (numMatch = 3)
                cout << "Congratulations you win 500!!!\n";
            
            else
                cout << "Sorry you did not pick the winning numbers, please try again.\n";
                
          
          
          system("pause");
          return 0;
    }

  5. #5
    Registered User
    Join Date
    Aug 2003
    Posts
    1,203
    Code:
    for (count =0; count < SIZE; count++)
            {
               while (numMatch && count < SIZE)
                {
                    if (user[count] != lottery [count])
                        numMatch = false;
                    count++;
                }
            }
    
    
            if (numMatch)
                cout << "Congratulations you are the Grand Prize Winner!!!\n";
            else if (numMatch = 4)
                cout << "Congratulations you win 1,000!!!\n";
            else if (numMatch = 3)
                cout << "Congratulations you win 500!!!\n";
            
            else
                cout << "Sorry you did not pick the winning numbers, please try again.\n";
    First of all, the red marked code you are trying to compare a bool with a number (actually you are assigning a number but I get the gist of it). Second the green marked code does not do what you (probably) think it does.
    Ameone likes this.

  6. #6
    Registered User
    Join Date
    Nov 2012
    Posts
    3
    Thank you, but how can I fix this? how can I make the program print 25 grand for all the numbers match, and 1,000 to 4 numbers match and 500 to 3 numbers match?

  7. #7
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,425
    A while loop is unneccesary, use an inner for loop instead - make numMatch an integer (or unsigned seeing as it is never meant to be <0 ) and add one to it every time a match is found
    The outer loop can start with the first lottery number then in the inner loop you compare aginst each of you user numbers, if its a match - numMatch++.
    When you have compared all the lottery numbers against all of your user numbers then check if the total in numMatch is equal to that required to win a prize

    Of course you need to guanrantee your user has not repeated any numbers in their selection and the same goes for the lottery numbers
    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'"

Popular pages Recent additions subscribe to a feed

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