Thread: Yet another lottery program

  1. #1
    Registered User
    Join Date
    Feb 2004
    Posts
    26

    Yet another lottery program

    I've looked through the board and haven't found anything that might help me out with this function, so I decided to post.
    Below is a lottery program that asks for a number of tickets, whether you want the computer to generate numbers, or if you want to plug in your own. It then displays the winning numbers. All of this works fine.

    ***What I can't figure out is how to make a function that compares the winning numbers with the user's (manual or quickpick)numbers to see how many numbers match for each ticket. I've been playing with stuff on paper for the last week, and I honestly don't know where to start.***

    Below is the current working program minus the comparison function. I am aware that the program below can be improved and done a thousand different ways, but the way most of this is done below is how my professor wanted this, so please, if possible, just stick to the comparison function. And yes, my Professor is aware of my using this board, and he encourages outside help when available. Thanks in advance.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include<string.h>
    #include<ctype.h>
    #include<time.h>
    
    void computerGenerator();
    void winningNumbers();
    void inputstring( char *inputptr );
    void printarray(int * arrpt);
    void playChoice();
    void guess();
    void ending();
    
    char inputarray[512];
    char *inputptr = inputarray;
    
    int main()
    {
    
       char start;
       srand(time(NULL));
       int counter=0;
       char choice;
       int numPlay=0;
       int lotto[6][5];
       int r, c;
    
       printf("                 Welcome to the lotto game!!\n");
       printf("                 ------- -- --- ----- ------\n\n");
       printf("This game asks you to pick six numbers or if you prefer\n");
       printf("quickpick and then prints the winning number in ascending order\n\n");
       
       printf("\tAre you ready to start?(y/n)  ", &start);
       scanf("%c", &start);
       fflush(stdin);
    
         if (start == 'y')     // begin if "yes"
         {
             system("cls");
             printf("How many games would you like to play? \n");
             scanf("%d", &numPlay);
             fflush(stdin);
             system("cls");   // clear screen
    
             printf(" Would you like to chose your own numbers, or are you lazy and want\n");
             printf(" quick pick instead? \n\n Enter U for user input or Q for quickpick: ");
             scanf("%c", &choice);
             fflush(stdin);
    
    
          if (choice == 'q')     // begin if "yes"
          {
              system("cls");
                  printf("Your quick pick numbers are: \n");
                  printf("---- ----- ---- ------- ----\n");
              for( counter=numPlay; counter > 0;  counter--)
              {
                   computerGenerator();
              }//end for
          }    //end if
         else
         {
               system("cls");
               printf("Please enter in your 6 numbers: \n");
    
      for(r=0; r<numPlay; r++)
      {
             for(c=0; c < 6; c++)
            {
    
               scanf("%d", &lotto[c][r]);
            }
      }  //end for r
    
      system("cls");
      printf("Here are your %d hand-picked numbers:\n", numPlay);
      printf("---- --- ---- - ----------- --------\n");
        for(r=0; r<numPlay; r++)
        {
                for(c=0; c < 6; c++)
                {
                    printf("%2d ", lotto[c][r]);
                }
    
           printf("\n");
         }  //end for c
       }// end for
    
          winningNumbers();
    
       } //end if
           else                // begin if "no"
           {
              system("cls");   // clear screen
              printf("\n\n\n\n");
              printf("\t     Aww...you don't want to try my program?\n");    // ending
              printf("\tWho needs you anyway, huh? Go buy a real ticket.\n\n");
              printf("\tAnother project brought to you by Me\n\n");
           }   // end else
    
       printf("\n");
       system("PAUSE");
       return 0;
    
    }   //end main
    
    
    
    void inputstring( char *inputptr )
    {
        while ( ( *inputptr++ = getchar() ) != '\n' )
           {
             //printf("%c", (*inputptr));
            }
            //        printf("\n");
            *( --inputptr ) = '\0';
    }
    
    
    void computerGenerator()
    {
       int array[6]={0};
       int arrlen;
    
       arrlen = sizeof(array)/sizeof(int);
    
       for(int x=0; x < arrlen; x++ )
       {
           array[x] = (int) rand()%53+1;
           for(int y=0; y<x; y++)
           {
              if (array[y] == array[x])
              {
                 array[x] = (int) rand()%53+1;
                 y=0;
              }  //if()
           }   //for (y)
        }  //for (x)
    
        // sort array using swap and bubbles
    
        for(int y=0; y < arrlen-1; y++ )
        {
           for(int x=0; x < arrlen-1; x++ )
           {
              if (array[x] > array[x+1])
              {
                 int temp;
                 temp = array[x];
                 array[x] = array [x+1];
                 array [x+1] =temp;
              }  //
           }   //
        }  //
    
        printarray(array);
    }   // end generator
    
    void winningNumbers()
    {
       int array[6]={0};
       int arrlen;
    
       arrlen = sizeof(array)/sizeof(int);
    
       for(int x=0; x < arrlen; x++ )
       {
           array[x] = (int) rand()%53+1;
           for(int y=0; y<x; y++)
           {
              if (array[y] == array[x])
              {
                 array[x] = (int) rand()%53+1;
                 y=0;
              }  //if()
           }   //for (y)
        }  //for (x)
    
        // sort array using swap and bubbles
    
        for(int y=0; y < arrlen-1; y++ )
        {
           for(int x=0; x < arrlen-1; x++ )
           {
              if (array[x] > array[x+1])
              {
                 int temp;
                 temp = array[x];
                 array[x] = array [x+1];
                 array [x+1] =temp;
              }  //
           }   //
        }  //
        printf("\n\nThe winning lotto numbers are: \n");
        printf("--- ------- ----- ------- ---- \n");
        printarray(array);
    }   // end generator
    
    void printarray(int*arrpt)
    {
    
       for (int x=0; x<6; x++)
       {
           printf("%2d ", arrpt[x]);
       }
       printf("\n");
    }      // end printarray
    
    void guess()
    {
        int play[6]={0};
        printf("Enter the six numbers you want to play\n");
        printf("Type them in separating by a space\n");
    
        inputstring( inputptr );
        system("CLS");
    }
    
    void ending()
    {
         int iochar;
         printf("\nYour hand-picked numbers are:  ");
         printf( inputarray );
         printf("\n");
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well, assuming you can't have duplicate numbers in a ticket:

    Winning numbers are: 4, 5, 5, <--- like that.

    Then simply loop through them and compare each number to the others in the list. Let's use arrays, because that's easy.
    Code:
    int winningnumbers[6] = { 1, 5, 33, 17, 45, 11 };
    int yourpick[6] = { 3, 5, 17, 22, 55, 12 };
    int x=0, y=0,matchingnumbers=0;
    
    for( x = 0; x < 6; x++ )
        for( y = 0; y < 6; y++ )
            if( yourpick[y] ==winningnumbers[x] )
                matchingnumbers++;
    
    printf("You have %d matching numbers.\n", matchingnumbers );
    Usually I don't do the whole thing for you, but this is fairly simple, and should get you started. You'll probably want to tidy it up a bit.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,212
    Quote Originally Posted by BungleSpice
    Code:
    int main()
    Code:
    int main(void)
    Code:
       char start;
       srand(time(NULL));
       int counter=0;
       char choice;
       int numPlay=0;
       int lotto[6][5];
       int r, c;
    Code:
       char start;
       int counter=0;
       char choice;
       int numPlay=0;
       int lotto[6][5];
       int r, c;
       srand(time(NULL));
    Code:
       
    fflush(stdin);
    I give up.

    First you have to decide whether you're writing c++ or c, then you have to realise why fflush(stdin) is bad.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Brian
    [code]
    First you have to decide whether you're writing c++ or c, then you have to realise why fflush(stdin) is bad.
    Actually, if they're using C99, the only thing incorrect is flushing stdin. Everything else is acceptable under C99. Point of reference: "C: A reference Manual, 5th edition", page 303 regarding main.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Feb 2004
    Posts
    26
    Quzah - Thanks for getting me started. It was a big help, and I'm most grateful.

    Brian...Please allow me to quote myself concerning my request for help.

    I am aware that the program below can be improved and done a thousand different ways, but the way most of this is done below is how my professor wanted this, so please, if possible, just stick to the comparison function.
    All of this works fine.
    It would appear that you did not thoroughly read my explanation. I was only looking for help to get started on a function to compare two arrays. I almost didn't post the rest of the code, but I decided to anyway just in case someone might find it helpful. I'm aware why fflush is wrong, and as I have stated, I know there are other ways to go about this, but so far, my program works and my teacher approves. This wasn't my problem, concern, or question, so it should be for you either. That is why I specifically said to ignore the rest.
    There is no need to get snotty. I've only been doing this a little over 2 months. I don't act like I know everything, and you shouldn't act like I do, either. If you don't have helpful comments, then you shouldn't have any at all.

  6. #6
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Quote Originally Posted by BungleSpice
    I'm aware why fflush is wrong, and as I have stated, I know there are other ways to go about this, but so far, my program works and my teacher approves. This wasn't my problem, concern, or question, so it should be for you either. That is why I specifically said to ignore the rest.
    I for one would be interested in your teacher's response when you pointed out that fflush(stdin) should not be used and why. And just because it "wasn't my problem, concern, or question" does not mean it won't be pointed out by those that know, and would like to teach you the proper way to write code. Esp. since an instructor that teaches fflush(stdin) is teaching bad programming practices. We're just trying to help...
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  7. #7
    Registered User
    Join Date
    Feb 2004
    Posts
    72
    Quote Originally Posted by BungleSpice
    . I'm aware why fflush is wrong, and as I have stated, I know there are other ways to go about this, but so far, my program works and my teacher approves. This wasn't my problem, concern, or question, so it should be for you either. That is why I specifically said to ignore the rest.
    There is no need to get snotty. I've only been doing this a little over 2 months. I don't act like I know everything, and you shouldn't act like I do, either. If you don't have helpful comments, then you shouldn't have any at all.
    If you practise good habits now then it will save you a LOT of pain in the future. Brian was trying to help fix some of the problems which, down the line, may be very hard to debug. You were not writing in mainstream C style and while it may have been legal C99, it is C++ style ("Brand new features such as // comments ... in C ... are guaranteed to cause trouble", Practice of Programming, Kernighan & Pike).

    If your professor absolutely insists that you write in this style then I suggest you add lots of comments explaining that what you wrote is wrong and providing a suitable alternative. It will help you a great deal in the future.

  8. #8
    Registered User
    Join Date
    Feb 2004
    Posts
    26
    i know that most people on the board are trying to help, and they have been a tremendous help to me in the past. My complaint was that Mr. Brian ignored my two requests: Help with a specific function, and to ignore the rest. He did neither. His comments were rude and unhelpful. It would seem that many people, once they get good at this stuff, become instant computer snobs and look down at all the newbies...myself being one of them. If he couldn't, or didn't want to help, he should have just ignored the post.

    If you would care to look at previous posts by me, you will notice that many conversations concerning my use of fflush haven been discussed in much detail. I, myself brought one up to find every alternative to fflush.
    Here are the cold, hard facts concerning my specific situation and the use of fflush:
    1. There is no easy, simple-to-use alternative
    2. I'm only doing this for college. I do not plan to continue with C after my graduation in May. I'm getting an A in the class, and that's good enough for me.
    3. fflush works just fine with my present compiler and programs. The program might not be portable, but I don't care because I am not taking it anywhere. It's only for the grade for a class I despise.
    4. My professor is aware and also gave alternatives to the class concerning this issue. This is the easiest way and he understands our situation so if we want to use fflush, he doesn't care either way.
    I suggest you add lots of comments explaining that what you wrote is wrong and providing a suitable alternative.
    I purposely left out the word fflush because in almost every single post that I specifically said "please ignore the fflush, I know all about it" people instantly jump on that subject anyway and ignore my request for help with another part and I never get the help I need. Then I get in the same off-topic conversation I have already had 10 times. I found it's better to not use the f-word (fflush) and just ask people to ignore any other parts they don't agree with. Still didn't work.

    All I am learning is that if I'm going to use fflush (even thought it is fine for my personal situation), then I shouldn't post anything on this board because people only want to talk about that subject and nothing else.

    I don't see myself continuing this subject. Another thing I have learned on this board is that people will read only what they want to read and they love to argue. They will keep baiting me and trolling me along to continue the argument, but ignore my whole point, which is always the exact same thing, "I'm only asking one specific question, please only help with that one specific question. "
    Just to prove my point, watch how many more people reply to this again telling me how I'm just sooo wrong, despite my saying that I know I'm wrong, and here are my reasons for doing it a different way, completely not getting my whole point.

  9. #9
    Registered User
    Join Date
    Feb 2004
    Posts
    72
    Quote Originally Posted by BungleSpice
    Just to prove my point, watch how many more people reply to this again telling me how I'm just sooo wrong, despite my saying that I know I'm wrong, and here are my reasons for doing it a different way, completely not getting my whole point.
    OK. Consider it from a different perspective. When you post code, people here may try to compile it so that they can help you out. If you post code which isn't mainstream C (i.e. C++ style comments (//) as opposed to C style comments (/**/), expressions which give undefined behaviour (fflush(stdin)), declarations in the middle of a block) then people may have to edit your code to allow them to compile it.

    In the end, irrespective of who is right and who is wrong, if you post code conforming to the guidelines suggested by Brian (and the FAQS) and what is generally regarded as 'mainstream' C then you won't be harming yourself, only helping yourself, helping others and avoiding a discussion about fflush(stdin) every time you post. Otherwise you know what consequences to expect.

    The choice is yours.

  10. #10
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Quote Originally Posted by BungleSpice
    ***What I can't figure out is how to make a function that compares the winning numbers with the user's (manual or quickpick)numbers to see how many numbers match for each ticket. I've been playing with stuff on paper for the last week, and I honestly don't know where to start.***
    Problem 1: There is no indication that you even understand how to do the comparison function -- which to me seems unbelievable. Given two lists of numbers, write down the steps you personally use to test the lists for identical values. Then refine these steps until you have a low-level step-by-step "algorithm". This algorithm could be really close to the code you're looking for -- it's just in words, not code.

    As for the "other" problem, you would be better off posting code segments that illustrate your difficlty instead of 200+ lines of code that doesn't have anything to do with the problem you're having and expecting us to take the time to understand this program with no guidance.

    And hundreds of Q'a and A's are posted to these boards daily. Are you asking us for your posts to specifically remember that your instructor is a beanhead and to not comment on bad practices? Heck, I can barely remember a comment I made yesterday, let alone your personal plight in the programming scene. Consider us in your tirade, which is also somewhat rude. Edit out the crap we are likely to comment on, save yourself and us some trouble.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  11. #11
    Registered User
    Join Date
    Feb 2004
    Posts
    26
    I got the help I needed in my very first reply. He understood what my question was and what it was not and adressed only my concern. I no longer have any questions on the function. I already explained why I added the rest of my code. It was only as a reference if it was needed, but it wasn't necessary to do even look at it if you didn't want to because it wasn't my question.

    The only reason I responded to anything after I received the first reply is because it was incredibly rude in the way that it was worded, regardless as to whether he was right or not. There was no justification for getting attitude because I didn't understand something. If I understood it, I wouldn't be here asking for help.

    As Quizah's first reply left me with no questions and provided the help I needed to get started, I consider this thread finished. I already said this before: I'm done with the subject. Save your replies and your keyboard.
    Let it die.

    Oh yeah:
    Just to prove my point, watch how many more people reply to this again telling me how I'm just sooo wrong, despite my saying that I know I'm wrong, and here are my reasons for doing it a different way, completely not getting my whole point.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  2. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 10:58 PM
  3. Lottery/ gambling opinions
    By rogster001 in forum C Programming
    Replies: 1
    Last Post: 08-21-2006, 06:04 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM