CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Sep 2014
    Posts
    4

    Issues while programming in notepad ++

    Hi everyone! I am trying to complete a homework assignment where I have to code a lottery machine in c# (I posted here because I have heard that C# and C++ are basically the same). I am using notepad++ and cmd prompt. The instructions are in the coding below. I "fixed" the machine to always output 1 1 1 (for testing purposes). I can't seems to get the correct output for when a user enters 3 correct numbers (in wrong order) or when a user enters 3 correct numbers (in the right order). Could you please help me find the issue?

    Thanks!

    using System;
    class Lottery
    {
    static void Main()
    {
    //declare variables
    Random ranNumberGenerator = new Random();
    int r1, r2, r3, n1, n2, n3;
    int orderR1 = 0;
    int orderR2 = 0;
    int orderR3 = 0;
    int correctR1 = 0;
    int correctR2 = 0;
    int correctR3 = 0;
    int numberCorrect = 0;
    int orderCorrect = 0;


    //generate random numbers
    r1 = ranNumberGenerator.Next(1,4);
    r2 = ranNumberGenerator.Next(1,4);
    r3 = ranNumberGenerator.Next(1,4);

    r1 = 1;
    r2 = 1;
    r3 = 1;

    //display instructions
    Console.WriteLine("This is a random number generator. You will be asked to enter three numbers. If your guesses match any of the numbers that the computer has generated you will receive a reward. You will earn better prices for matching more numbers and for guessing the order of the numbers correctly. Good Luck!");
    //read user input
    //n1
    Console.WriteLine("-Please enter your first number between 1 and 4.");
    n1 = Convert.ToInt32(Console.ReadLine());

    //n2
    Console.WriteLine("-Please enter your second number between 1 and 4.");
    n2 = Convert.ToInt32(Console.ReadLine());

    //n3
    Console.WriteLine("-Please enter your third number between 1 and 4.");
    n3 = Convert.ToInt32(Console.ReadLine());

    //output results
    Console.WriteLine("The lottery numbers are:{0}, {1}, {2}", r1,r2,r3);
    Console.WriteLine("You entered:{0}, {1}, {2}", n1,n2,n3);

    //compute results
    if ((r1 == n1) || (r1 == n2) || (r1 == n3))
    {
    correctR1 = 1;
    if (r1 == n1)
    {
    n1 = 0;
    orderR1 = 1;
    }
    else
    if (r1 == n2)
    {
    n2 = 0;
    }
    else
    if (r1 == n3)
    {
    n3 = 0;
    }
    else
    {
    correctR1 = 0;
    }
    }
    if ((r2 == n1) || (r2 == n2) || (r2 == n3))
    {
    correctR2 = 1;
    if (r2 == n1)
    {
    n1 = 0;
    }
    else
    if (r2 == n2)
    {
    n2 = 0;
    orderR2 = 1;
    }
    else
    if (r2 == n3)
    {
    n3 = 0;
    }
    else
    {
    correctR2 = 0;
    }
    }
    if ((r3 == n1) || (r3 == n2) || (r3 == n3))
    {
    correctR3 = 1;
    if (r3 == n1)
    {
    n1 = 0;
    }
    else
    if (r3 == n2)
    {
    n2 = 0;
    }
    else
    if (r3 == n3)
    {
    n3 = 0;
    orderR3 = 1;
    }
    else
    {
    correctR3 = 0;
    }
    }

    else
    {
    //calculate number correct
    numberCorrect = correctR1 + correctR2 + correctR3;
    if (numberCorrect == 0)
    {
    Console.WriteLine("Sorry, none of your numbers matched the computer's.");
    }
    else
    if (numberCorrect == 1)
    {
    Console.WriteLine("One of your number's matched, you win $10!");
    }
    else
    if (numberCorrect == 2)
    {
    Console.WriteLine("Two of number's matched, you win $100!");
    }
    else
    if (numberCorrect == 3)
    {
    //check for correct order
    orderCorrect = orderR1 + orderR2 + orderR3;
    if (orderCorrect == 3)
    {
    Console.WriteLine("All of your numbers matched and were in the correct order!");
    Console.WriteLine("You win $10,000!!!");
    }
    else
    {
    Console.WriteLine("Three of your number's matched, you win $1000!");
    }
    }

    }
    }
    }

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Issues while programming in notepad ++

    Doesn't make much sense to post in a C++ forum just because it's similar to to C#. The C# forum is the appropriate place.

    Please use code tags to preserve formatting.

    The debugger shows you clearly what the issue is. You'll find that the else here is what's throwing you off. The results section of your code should be executed unconditionally.

    Code:
    correctR3 = 0;
    }
    }
    
    else
    {
    //calculate number correct

  3. #3
    Join Date
    Sep 2014
    Posts
    4

    Re: Issues while programming in notepad ++

    Quote Originally Posted by GCDEF View Post
    Doesn't make much sense to post in a C++ forum just because it's similar to to C#. The C# forum is the appropriate place.

    Please use code tags to preserve formatting.

    The debugger shows you clearly what the issue is. You'll find that the else here is what's throwing you off. The results section of your code should be executed unconditionally.

    Code:
    correctR3 = 0;
    }
    }
    
    else
    {
    //calculate number correct
    Thank you for helping me GCDEF, I understand what I was doing wrong now. Everything seems to work like it should.
    Sorry for posting in the wrong subforum. Also, I am completely new to C#, what do you use for debugging? Thanks!

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Issues while programming in notepad ++

    Quote Originally Posted by ranger133 View Post
    Also, I am completely new to C#, what do you use for debugging? Thanks!
    Definitely not Notepad.

    You can get the Visual Studio 2013 Express version from Microsoft for free:

    Microsoft Visual Studio Express 2013 with Update 3 for Windows Desktop

    It includes an Express version of C# with a fully integrated IDE and debugger.

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