CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Dec 2011
    Posts
    1

    C# Lottery console application NEED HELP!

    Hello, I am having problem with my console application. I don't know how to check if my inputted numbers matches with the random number generator. Also I don't know how to specify the inputted number that matches the number matches prize draw. Here is my code.

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    
    namespace random
    {
        class Program
        {
            static void Main(string[] args)
            {
                // declaring integers
                int iNum1;
                int iNum2;
                int iNum3;
                int iNum4;
                int iNum5;
                int iNum6;
                int iCount = 0;
                int iGuess;
                int iRandom;
                int iAmount;
                // declaring constants
    
                const int iBALLS = 6;
                const int iMATCHONE = 10;
                const int iMATCHTWO = 20;
                const int iMATCHTHREE = 40;
                const int iMATCHFOUR= 80;
                const int iMATCHFIVE = 140;
                const int iMATCHSIX = 1000;
    
                // array
                
    
                // generating random numbers
    
                Random randomnumber = new Random();
    
                iNum1 = randomnumber.Next(1, 49);
                iNum2 = randomnumber.Next(1, 49);
                iNum3 = randomnumber.Next(1, 49);
                iNum4 = randomnumber.Next(1, 49);
                iNum5 = randomnumber.Next(1, 49);
                iNum6 = randomnumber.Next(1, 49);
    
                iRandom = iNum1 + iNum2 + iNum3 + iNum4 + iNum5 + iNum6;
    
                for (iCount = 0; iCount < iBALLS; iCount++)
                {
                    
                    Console.Write("No:" + (iCount + 1) + " Choose your number: ");
                    iGuess = Convert.ToInt32(Console.ReadLine());
                    
    
                    while (iGuess < 1 || iGuess > 10)
                    {
                        Console.WriteLine("You have entered an incorrect number please try again");
                        iGuess= Convert.ToInt32(Console.ReadLine());
    
                    }
    
                    iAmount = 0;
    
                    if (iGuess == iNum1)
                    {
                        Console.WriteLine("you won $" + iMATCHONE);
                        Console.WriteLine();
                        Console.WriteLine("Press any key to continue");
                        Console.ReadKey();
                        iAmount++;
                    }
    
    
                    if (iGuess == iNum2)
                    {
                        Console.WriteLine("you won $" + iMATCHTWO);
                        Console.WriteLine();
                        Console.WriteLine("Press any key to continue");
                        Console.ReadKey();
                        iAmount++;
                    }
    
                    if (iGuess == iNum3)
                    {
                        Console.WriteLine("you won $" + iMATCHTHREE);
                        Console.WriteLine();
                        Console.WriteLine("Press any key to continue");
                        Console.ReadKey();
                        iAmount++;
                    }
    
                    if (iGuess == iNum4)
                    {
                        Console.WriteLine("you won $" + iMATCHFOUR);
                        Console.WriteLine();
                        Console.WriteLine("Press any key to continue");
                        Console.ReadKey();
                        iAmount++;
                    }
    
    
                    if (iGuess == iNum5)
                    {
                        Console.WriteLine("you won $" + iMATCHFIVE);
                        Console.WriteLine();
                        Console.WriteLine("Press any key to continue");
                        Console.ReadKey();
                        iAmount++;
                    }
    
    
                    if (iGuess == iNum6)
                    {
                        Console.WriteLine("you won $" + iMATCHSIX);
                        Console.WriteLine();
                        Console.WriteLine("Press any key to continue");
                        Console.ReadKey();
                        iAmount++;
                    }
    
    
                    Console.WriteLine("you have matched " + iAmount + " numbers");
                }
    
    
    
                Console.WriteLine("The numbers are " + iNum1 + ", " + iNum2 + ", " + iNum3 + ", " + iNum4 + ", " + iNum5 + ", " + iNum6 + ", ");
                Console.ReadKey();
    
    
            }
        }
    }
    Last edited by cjard; December 3rd, 2011 at 06:08 AM.

  2. #2
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: C# Lottery console application NEED HELP!

    For starters you would need to test each entry against all of the random numbers or up until a match is found. Assuming of course the intention is not to force them to guess them in the same order as generated which it would do now.

    When posting code use [code][/code] tags. To hard to read without proper formatting. Also state your question clearly. You may know what you are asking but when you do not ask a clear question most people will not know what you are asking.
    Always use [code][/code] tags when posting code.

  3. #3
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104

    Re: C# Lottery console application NEED HELP!

    When you start learning to program, first write the program in english (or your native language) then translate it into code.. write it in comments, that way you already have a commented program when you finish. Here's an idea. I've written the logic of the program for you in English, You translate it to code. We do thios because all our lives we've beein thinking in English, not C#.. Think in what you know, then translate

    Code:
    //declare an array to hold the 6 guesses
    
    //declare an array to hold 6 random numbers
    
    //write a for loop to loop over the guesses array. on each loop it will ask the user for a number
    //end the loop
    
    //make a random numnber generator
    
    //write another loop to call the random generator 6 times and store 6 random numbers in the other array
    
    //declare a number to hold the count of correct numbers the user guessed
    
    //write two loops, one inside the other
      //the outer loop will grab an element of the guesses the user made
        //the inner loop will check every element of the random numbers the computer picked
          //if a match is found, increase the c"correct numbers" counter by one, and exit the inner loop
      //end of inner loop
    //end of outer loop
    
    //we have now, for every guess the user made, checked it against all the numbers the computer picked and counted the number of matches
    //do something with the number that matched, tell the user what they won
    After you finish this part, the only enhancement you would need to make is if either guesses or random numbers array already contains that number, ask the user for another or generate another random (we dont need to get into the complexity of generating 50 sequential numbers and removing one each time randomly to ensure that we have unique numbers.. Because the range is 1-50 and we only want 6, we can just get another random if we get a colliding number and probably the second attempt won't collide
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured