Thread: Needs help making a lottery

  1. #1
    Registered User
    Join Date
    Feb 2013
    Location
    Sweden
    Posts
    171

    Question Needs help making a lottery

    I have begun to make a lottery and the code likes this so far

    Code:
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    using namespace std;
    int main()
    {
        int myNum[7];
        int drawNum[7];
    
        for (int selectYN = 1; selectYN < 7; selectYN++)
        {
            cout << "Enter number " << selectYN << ": ";
            cin >> myNum[selectYN];
    
            if (myNum[selectYN] > 50)
            {
                do
                {
                cout << "You didn't enter a valid value! " << endl;
                cout << "Enter number " << selectYN << ": ";
                cin >> myNum[selectYN];
                } while (myNum[selectYN] > 50);
            }
        }
                cout << "Your line is: ";
                for (int enteredNums = 1; enteredNums < 7; enteredNums++)
                {
                    cout << myNum[enteredNums] << " ";
                }
        return 0;
    }
    The problem is that drawNum7 I want to call random numbers from it like:


    But that's not possible to do like that because this is an array and needs two {, } after =. I thought about that I could compare myNum7 with drawNum7 with a for loop then.

    Is it any way that I can say that all drawNum7 is random draw between 50 and 1?.

  2. #2
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    It is a bit unclear from your question what your problem is but i think you would like to have a random set of numbers drawn for your lottery?

    A simple way to get a random number is like this:

    Code:
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    
    int main()
    {
        srand(time(NULL));
    
        int rNum = 0;
    
        rNum = rand() % 50 + 1;
        
        std::cout << rNum << "\n";
        
    }
    Last edited by rogster001; 04-21-2013 at 02:07 AM.
    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'"

  3. #3
    Registered User
    Join Date
    Feb 2013
    Location
    Sweden
    Posts
    171
    Yes that's exactly what I mean. But you can not do it with an array. Like this: int rNum[7] = rand() % 50 + 1. That's the problem. I could have done something like rNum1, rNum2, rNum3. But I want a easier way if possible.

  4. #4
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by DecoratorFawn82 View Post
    Yes that's exactly what I mean. But you can not do it with an array. Like this: int rNum[7] = rand() % 50 + 1. That's the problem. I could have done something like rNum1, rNum2, rNum3. But I want a easier way if possible.
    you can do
    Code:
     int rNum[7];
     rNum[0] = rand() % 50 + 1. 
     rNum[1] = rand() % 50 + 1. 
    ....
    Leaves just the problem of duplicates,
    but you could put that into a function that checks if the number was already drawn.
    Kurt

  5. #5
    Registered User
    Join Date
    Feb 2013
    Location
    Sweden
    Posts
    171
    Does that work?. That didn't I know but thanks!. It should be possible to use a for loop to compare my numbers with the random numbers huh?. I guess so.
    Last edited by DecoratorFawn82; 04-21-2013 at 02:52 AM.

  6. #6
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    you can just use a function containing a loop to assign your aray the random numbers, supressing duplicates - a while loop is good for this. On the other hand as this is C++ you can use std::set
    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'"

  7. #7
    Registered User
    Join Date
    Feb 2013
    Location
    Sweden
    Posts
    171
    I suppose that this works well too.

    Code:
        int drawNum[7];
        srand(time(0));
        for (int i = 1; i < 7; i++)
        {
            drawNum[i] = rand() % 50 + 1;
            cout << drawNum[i] <<  " " << endl;
        }
    Last edited by DecoratorFawn82; 04-21-2013 at 04:42 AM.

  8. #8
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    yes but you could have the same number selected several times - you need work to prevent 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'"

  9. #9
    Registered User
    Join Date
    Feb 2013
    Location
    Sweden
    Posts
    171
    What's wrong with this code I need something to compare my numbers with random numbers.

    Code:
                for (int cmpNum1 = 1; myNum[cmpNum1] < 7; cmpNum1++)
                {
                    for (int cmpNum2 = 1; drawNum[cmpNum2] < 7; cmpNum2++)
                    {
                        if (myNum[cmpNum1] == drawNum[cmpNum2])
                        {
                            cout << "Yes";
                        }
                    }
                }

  10. #10
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by DecoratorFawn82 View Post
    What's wrong with this code ....
    You are not checking the first elements and your loop condition is wrong . should check for cmpNum1 and cmpNum2 < 7.
    Kurt
    Last edited by ZuK; 04-21-2013 at 05:31 AM.

  11. #11
    Registered User
    Join Date
    Feb 2013
    Location
    Sweden
    Posts
    171
    Oh, you mean that I check 1 < 7 instead of 0 < 6 huh?

    I have already tried with 0 < 6 too. But that won't work for me too.

    I changed the code and tried again but if you have entered 19 as the 6th number.
    And then has it at 3rd number at the random number line. Should the program say that you have one number correct then?
    Last edited by DecoratorFawn82; 04-21-2013 at 05:33 AM.

  12. #12
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    that code is hideous, use a constant to define the extent of each loop
    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'"

  13. #13
    Registered User
    Join Date
    Feb 2013
    Location
    Sweden
    Posts
    171
    A const int?. To give all loops a value of 6?. I don't understand how that make differens. If I not misunderstood you.

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by DecoratorFawn82
    A const int?. To give all loops a value of 6?. I don't understand how that make differens.
    By using a named constant instead of a magic number, you would make your code more readable and hence easier to understand. Furthermore, in the event that the number is to change, changing a constant is safer (and probably faster) than trying to change every relevant instance of a magic number.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by laserlight View Post
    By using a named constant instead of a magic number, you would make your code more readable and hence easier to understand. Furthermore, in the event that the number is to change, changing a constant is safer (and probably faster) than trying to change every relevant instance of a magic number.
    I would refer to
    SourceForge.net: Safer arrays in Cpp - cpwiki

    Anyway, the problem (with your code, which is not very good code) is that it should be something like
    for (int cmpNum1 = 0; cmpNum1 < 7; cmpNum1++)
    Indexes starts at 0, and you want to check every index.

    But I urge you to read the article above for some insight into arrays and problems.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Generating lottery numbers
    By les2012 in forum C++ Programming
    Replies: 13
    Last Post: 12-05-2012, 08:01 PM
  2. help with a lottery problem.
    By todouble22 in forum C++ Programming
    Replies: 17
    Last Post: 01-26-2010, 12:57 PM
  3. Lottery game
    By got1sleeve in forum C++ Programming
    Replies: 3
    Last Post: 02-16-2008, 01:55 PM
  4. Lottery game
    By geetard in forum C++ Programming
    Replies: 2
    Last Post: 12-20-2005, 12:50 AM
  5. Yet another lottery program
    By BungleSpice in forum C Programming
    Replies: 10
    Last Post: 04-01-2004, 02:08 PM