Need help with a lottery program

D3migodly 21 Reputation points
2022-01-13T16:05:22.263+00:00

I have to make a lottery program as a school project. I've done most of it but don't have any idea how to compare the user input to the computer-generated one.

This is the code:

using System;

namespace gamblinf
{
    class Program
    {
        public static void Main(string[] args)
        {
            int Match = 0; 
            int num = 0; 
            int num1 = 0; 
            Console.WriteLine("Type a number with 5 digits");   
            for (int a = 1; a < 6; a++) 
            { 
                num = Console.Read();
            }
            Console.ReadLine();
            Console.Write("The winning number is "); 

            for (int i = 1; i < 6; i++) 
            { 
                Random rnd = new Random(); 
                num1 = rnd.Next(0, 10);
                Console.Write(num1);
                if (num1 == num)
                { 
                    Match += 1;
                } 
            }
            Console.ReadLine();
            Console.WriteLine("You got " + Match + " numbers right."); 
            if (Match == 0)
            {
                Console.WriteLine("Better luck next time!");
            }
            if (Match == 1)
            {
                Console.WriteLine("Good, but could be a bit better!");
            }
            if (Match == 2 || Match == 3)
            {
                Console.WriteLine("Great! You just won 100 dollars!");
            }
            if (Match == 4)
            {
                Console.WriteLine("Amazing! You are now 10,000 dollars richer!");
            }
            if (Match == 5)
            {
                Console.WriteLine("Congratulations! You won the main prize of 1,000,000 dollars!");
            }
            if (Match < 0 || Match >5)
            {
                Console.WriteLine("What the heck");
            }
            Console.ReadLine(); 
        }
    }
}

That's about it. Cya.

Developer technologies C#
{count} votes

Accepted answer
  1. P a u l 10,761 Reputation points
    2022-01-13T17:15:53.34+00:00

    A few points:

    Should this be a single integer or an array?

       int num = 0;  
    

    I'd assume the inputs should be stored in an array (let's call it nums,) then in your second loop you'd do something like:

       if (nums.Contains(num1))  
    

    Instead of:

       if (num1 == num)  
    

    A second thing is that Console.Read returns the character code, rather than the character itself. Instead you could get an entire line (as a string) using Console.ReadLine, then parse it using int.TryParse:
    https://learn.microsoft.com/en-us/dotnet/api/system.int32.tryparse?view=net-6.0#:~:text=successfully%3B%20otherwise%2C%20false.-,Examples,-The%20following%20example

    There are some extra validation cases, such as making sure that all numbers entered in nums in the first loop, and numbers generated in the second loop are distinct, but I'll leave that as an exercise for the reader.


2 additional answers

Sort by: Most helpful
  1. Muzammal Hussain 0 Reputation points
    2023-07-31T05:18:43.0866667+00:00
    Hello! I see that you've created a lottery program in C#. It's a good start, but I'll help you fix the issues and guide you on how to compare the user input to the computer-generated winning number.
    
    Here are the changes you need to make:
    
    The primary issue is with how you read the user input. Instead of using Console.Read(), you should use int.Parse(Console.ReadLine()) to read a full integer input from the user.
    
    The loop for generating the random numbers should be outside the loop for user input. Otherwise, you are generating a new random number in each iteration of the user input loop, which will result in different numbers every time.
    
    Since you want a 5-digit number, you need to create an array to store each digit of the user input, as well as the winning number.
    
    You need to compare each digit of the user input to the corresponding digit of the winning number.
    
    Here's the modified code:
    
    
    using System;
    
    namespace gambling
    {
        class Program
        {
            public static void Main(string[] args)
            {
                int Match = 0;
                int[] userNumber = new int[5];
                int[] winningNumber = new int[5];
                Console.WriteLine("Type a number with 5 digits");
                
                // Read user input and store each digit in the array
                for (int a = 0; a < 5; a++)
                {
                    userNumber[a] = int.Parse(Console.ReadLine());
                }
                
                Console.Write("The winning number is ");
    
                // Generate the winning number and store each digit in the array
                Random rnd = new Random();
                for (int i = 0; i < 5; i++)
                {
                    winningNumber[i] = rnd.Next(0, 10);
                    Console.Write(winningNumber[i]);
                }
    
                // Compare user input with the winning number and count matches
                for (int i = 0; i < 5; i++)
                {
                    if (userNumber[i] == winningNumber[i])
                    {
                        Match += 1;
                    }
                }
    
                Console.WriteLine();
                Console.WriteLine("You got " + Match + " numbers right.");
    
                // Display the corresponding prize based on the number of matches
                if (Match == 0)
                {
                    Console.WriteLine("Better luck next time!");
                }
                else if (Match == 1)
                {
                    Console.WriteLine("Good, but could be a bit better!");
                }
                else if (Match == 2 || Match == 3)
                {
                    Console.WriteLine("Great! You just won 100 dollars!");
                }
                else if (Match == 4)
                {
                    Console.WriteLine("Amazing! You are now 10,000 dollars richer!");
                }
                else if (Match == 5)
                {
                    Console.WriteLine("Congratulations! You won the main prize of 1,000,000 dollars!");
                }
                else
                {
                    Console.WriteLine("What the heck");
                }
    
                Console.ReadLine();
            }
        }
    }
    Now, the code will read the user input correctly, generate a random winning number, compare the two, and provide the appropriate result. It should work as expected. If you have any further questions or need more assistance, feel free to ask. Good luck with your school project! 
    

  2. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.