Sam Rose

Sam Rose

  • NA
  • 3
  • 0

Stack Overflow Exception

Aug 13 2009 9:31 AM
Hi, I'm a novice at the C# language and I've run into a bit of a problem. I'm writing a program that simulates the national lottery for a bit of fun and it has a loop in it that is going to need to repeat itself, probably millions of times. This seems to consistently give me Stack Overflow Exceptions though. I was wondering if there was any way to stop this from happening? Here's my code:

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{

class Program
{
int tries, numbersmatched, sel1, sel2, sel3, sel4, sel5, sel6, num1, num2, num3, num4, num5, num6;
Random random = new Random();


public void Menu()
{
LotteryNumbers();
UserNumbers();
Console.WriteLine("Lottery numbers: {0}, {1}, {2}, {3}, {4}, {5}", num1, num2, num3, num4, num5, num6);
Console.WriteLine("Your numbers: {0}, {1}, {2}, {3}, {4}, {5}", sel1, sel2, sel3, sel4, sel5, sel6);
CheckWin();
Console.WriteLine("You matched " + numbersmatched + " numbers.");
tries++;
Menu();
}

public void UserNumbers()
{
sel1 = random.Next(1, 49);
sel2 = random.Next(1, 49);
while (sel1 == sel2)
{
sel2 = random.Next(1, 49);
}
sel3 = random.Next(1, 49);
while (sel1 == sel3 || sel2 == sel3)
{
sel3 = random.Next(1, 49);
}
sel4 = random.Next(1, 49);
while (sel1 == sel4 || sel2 == sel4 || sel3 == sel4)
{
sel4 = random.Next(1, 49);
}
sel5 = random.Next(1, 49);
while (sel1 == sel5 || sel2 == sel5 || sel3 == sel5 || sel4 == sel5)
{
sel5 = random.Next(1, 49);
}
sel6 = random.Next(1, 49);
while (sel1 == sel6 || sel2 == sel6 || sel3 == sel6 || sel4 == sel6 || sel5 == sel6)
{
sel6 = random.Next(1, 49);
}
}

public void LotteryNumbers()
{
num1 = random.Next(1, 49);
num2 = random.Next(1, 49);
while (num1 == num2)
{
num2 = random.Next(1, 49);
}
num3 = random.Next(1, 49);
while (num1 == num3 || num2 == num3)
{
num3 = random.Next(1, 49);
}
num4 = random.Next(1, 49);
while (num1 == num4 || num2 == num4 || num3 == num4)
{
num4 = random.Next(1, 49);
}
num5 = random.Next(1, 49);
while (num1 == num5 || num2 == num5 || num3 == num5 || num4 == num5)
{
num5 = random.Next(1, 49);
}
num6 = random.Next(1, 49);
while (num1 == num6 || num2 == num6 || num3 == num6 || num4 == num6 || num5 == num6)
{
num6 = random.Next(1, 49);
}
}

public void CheckWin()
{
numbersmatched = 0;
if (sel1 == num1 || sel1 == num2 || sel1 == num3 || sel1 == num4 || sel1 == num5 || sel1 == num6)
{
numbersmatched++;
}
if (sel2 == num1 || sel2 == num2 || sel2 == num3 || sel2 == num4 || sel2 == num5 || sel2 == num6)
{
numbersmatched++;
}
if (sel3 == num1 || sel3 == num2 || sel3 == num3 || sel3 == num4 || sel3 == num5 || sel3 == num6)
{
numbersmatched++;
}
if (sel4 == num1 || sel4 == num2 || sel4 == num3 || sel4 == num4 || sel4 == num5 || sel4 == num6)
{
numbersmatched++;
}
if (sel5 == num1 || sel5 == num2 || sel5 == num3 || sel5 == num4 || sel5 == num5 || sel5 == num6)
{
numbersmatched++;
}
if (sel6 == num1 || sel6 == num2 || sel6 == num3 || sel6 == num4 || sel6 == num5 || sel6 == num6)
{
numbersmatched++;
}
}
static void Main(string[] args)
{
Program Lottery = new Program();
Lottery.tries = 0;
Lottery.Menu();
}


}

}

Any help is appreciated :) Thanks!

Answers (2)