473,385 Members | 2,162 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Program crashes after a few executions

Hello,

i am learning c# and have created now a simple project that just creates 6
random numbers.
My form includes a button and 6 labels for the random numbers. The program
seems to work correct,
however when after a few button clicks the program crashes.

Does anyone know where the reason could be?

--
Kind regards
Thomas Neubauer

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void btZieheLottozahlen_Click(object sender, EventArgs e)
{
int[] Zahlen = new int[6];
Random Zufallszahl = new Random();
int Doppelte = 0;

do
{
for (int i = 0; i < 6; i++)
{
Zahlen[i] = Zufallszahl.Next(1, 64);
}
Array.Sort(Zahlen);
for (int i = 0; i < 5; i++)
if (Zahlen[i] == Zahlen[i + 1])
Doppelte = 1;

} while (Doppelte == 1);

label1.Text = Convert.ToString(Zahlen[0]);
label2.Text = Convert.ToString(Zahlen[1]);
label3.Text = Convert.ToString(Zahlen[2]);
label4.Text = Convert.ToString(Zahlen[3]);
label5.Text = Convert.ToString(Zahlen[4]);
label6.Text = Convert.ToString(Zahlen[5]);
}
}
}
Nov 20 '07 #1
13 1605
Thomas Neubauer <ng****@vol.atwrote:
i am learning c# and have created now a simple project that just creates 6
random numbers.
My form includes a button and 6 labels for the random numbers. The program
seems to work correct,
however when after a few button clicks the program crashes.

Does anyone know where the reason could be?
Well, in what way does it crash? Is there an exception?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
Nov 20 '07 #2
You don't reset Doppelte to 0 at the start of a loop; once you have
hit a duplicate it will loop forever.

Marc
Nov 20 '07 #3
On 2007-11-20 14:01:12 -0800, "Thomas Neubauer" <ng****@vol.atsaid:
Hello,

i am learning c# and have created now a simple project that just
creates 6 random numbers.
My form includes a button and 6 labels for the random numbers. The
program seems to work correct,
however when after a few button clicks the program crashes.

Does anyone know where the reason could be?
I don't see anything obvious in the code you posted that would cause a crash.

Which is not to say the code is bug-free:

* I wouldn't send an int in to do a bool's job, but that problem
shouldn't cause your program to crash.

* Perhaps more distressingly, you never reset "Doppelte" in your
loop, so if you ever fail to have an array of unique random numbers,
your loop will never exit.

The latter problem would cause your program to stop responding, but a)
I wouldn't call that a "crash", and b) if you interrupt the program in
the debugger you would easily see where the program is stuck, and you
could also just as easily step through the code to see what happens to
the variables and hopefully notice when one isn't behaving as you
expect it to.

The debugger is there to help you. You should use it. :)

Still, you haven't really been very specific in your post. If the
above information doesn't help, what does "crash" mean? Are you
getting an exception? If so, what is the exception? On what line of
code does it occur? What other code is part of your application?

Pete

Nov 20 '07 #4
I love it!

"Peter Duniho" wrote:
On 2007-11-20 14:01:12 -0800, "Thomas Neubauer" <ng****@vol.atsaid:
Hello,

i am learning c# and have created now a simple project that just
creates 6 random numbers.
My form includes a button and 6 labels for the random numbers. The
program seems to work correct,
however when after a few button clicks the program crashes.

Does anyone know where the reason could be?

I don't see anything obvious in the code you posted that would cause a crash.

Which is not to say the code is bug-free:

* I wouldn't send an int in to do a bool's job, but that problem
shouldn't cause your program to crash.

* Perhaps more distressingly, you never reset "Doppelte" in your
loop, so if you ever fail to have an array of unique random numbers,
your loop will never exit.

The latter problem would cause your program to stop responding, but a)
I wouldn't call that a "crash", and b) if you interrupt the program in
the debugger you would easily see where the program is stuck, and you
could also just as easily step through the code to see what happens to
the variables and hopefully notice when one isn't behaving as you
expect it to.

The debugger is there to help you. You should use it. :)

Still, you haven't really been very specific in your post. If the
above information doesn't help, what does "crash" mean? Are you
getting an exception? If so, what is the exception? On what line of
code does it occur? What other code is part of your application?

Pete

Nov 20 '07 #5
Sorry, I never even saw the code, and thought you were making up terms like
Doppelte... Long day at the office...

"Family Tree Mike" wrote:
I love it!

"Peter Duniho" wrote:
On 2007-11-20 14:01:12 -0800, "Thomas Neubauer" <ng****@vol.atsaid:
Hello,
>
i am learning c# and have created now a simple project that just
creates 6 random numbers.
My form includes a button and 6 labels for the random numbers. The
program seems to work correct,
however when after a few button clicks the program crashes.
>
Does anyone know where the reason could be?
I don't see anything obvious in the code you posted that would cause a crash.

Which is not to say the code is bug-free:

* I wouldn't send an int in to do a bool's job, but that problem
shouldn't cause your program to crash.

* Perhaps more distressingly, you never reset "Doppelte" in your
loop, so if you ever fail to have an array of unique random numbers,
your loop will never exit.

The latter problem would cause your program to stop responding, but a)
I wouldn't call that a "crash", and b) if you interrupt the program in
the debugger you would easily see where the program is stuck, and you
could also just as easily step through the code to see what happens to
the variables and hopefully notice when one isn't behaving as you
expect it to.

The debugger is there to help you. You should use it. :)

Still, you haven't really been very specific in your post. If the
above information doesn't help, what does "crash" mean? Are you
getting an exception? If so, what is the exception? On what line of
code does it occur? What other code is part of your application?

Pete
Nov 20 '07 #6
On 2007-11-20 14:57:00 -0800, Family Tree Mike
<Fa************@discussions.microsoft.comsaid:
Sorry, I never even saw the code, and thought you were making up terms like
Doppelte... Long day at the office...
lol...I was wondering what I'd written that made you so happy.

Still, sounds like something funny to do some day. Maybe next April 1,
we should just all answer questions by sprinkling in made-up words
throughout. :)

Nov 20 '07 #7
Hello all,

thanks to all for your very fast help. I haven't expected such fast help, so
i was really surprised.
I meant that my program doesn't react anymore after a few clicks. Probably
"crash" was the wrong word.
Anyway i solved through setting variable "Doppelte" to 0 on the test of the
last array element.

do
{
for (int i = 0; i < 6; i++)
{
Zahlen[i] = Zufallszahl.Next(1, 64);
}
Array.Sort(Zahlen);
for (int i = 0; i < 5; i++)
if (Zahlen[i] == Zahlen[i + 1])
Doppelte = 1;
else
if (Zahlen[i] == 4)
Doppelte = 0;

} while (Doppelte == 1);

--
Kind regards
Thomas Neubauer

Nov 20 '07 #8
On 2007-11-20 15:23:44 -0800, "Thomas Neubauer" <ng****@vol.atsaid:
Hello all,

thanks to all for your very fast help. I haven't expected such fast
help, so i was really surprised.
I meant that my program doesn't react anymore after a few clicks.
Probably "crash" was the wrong word.
Anyway i solved through setting variable "Doppelte" to 0 on the test of
the last array element.
Yuck!

Why not just set it in the first line of the do/while loop? There's
not even a need to initialize in the declaration if you do that.

I would still change it to a bool, in any case.

Pete

Nov 20 '07 #9
Ditto that "Yuck!"

Actually you could make the whole thing a lot simpler... it looks like
you just want 6 sorted random numbers in a range with no duplicates...
so how about:

Random rand = new Random();
List<intfound = new List<int>();
while (found.Count < 6) {
int newNumber;
while (found.Contains(newNumber = rand.Next(1, 64)))
{ }
found.Add(newNumber);
}
found.Sort();
int[] array = found.ToArray(); // if you *need* an array

One advantage here is that it doesn't throw away everything just
because of a collision - it just dumps the duplicated value; this
might be significant if the numbers get larger. But more importantly
there are no complicated exit variables to set/reset - just "have we
got everything we need yet?"

Marc

Nov 21 '07 #10
And in true style I *still* over-complicated things!

Following is simpler and avoids nested loop;

while (found.Count < 6) {
int newNumber = rand.Next(1, 64);
if (!found.Contains(newNumber)) {
found.Add(newNumber);
}
}
Nov 21 '07 #11
Hi,

thanks to all for your excellent help!

BTW: Doppelte is a germany word. I'm sorry, but it was just one of my first
sample projects,
but of course, normally all my codes are in english.
--
Regards
Thomas Neubauer

Nov 21 '07 #12

-----Original Message-----
From: Marc Gravell [mailto:ma**********@gmail.com]
Posted At: Wednesday, 21 November 2007 2:32 PM
Posted To: microsoft.public.dotnet.languages.csharp
Conversation: Program crashes after a few executions
Subject: Re: Program crashes after a few executions

And in true style I *still* over-complicated things!

Following is simpler and avoids nested loop;
I must admit I was wondering why you had done it the first way, it
seemed overly complicated ;)

Nov 22 '07 #13
-----Original Message-----
From: Thomas Neubauer [mailto:ng****@vol.at]
Posted At: Thursday, 22 November 2007 8:04 AM
Posted To: microsoft.public.dotnet.languages.csharp
Conversation: Program crashes after a few executions
Subject: Re: Program crashes after a few executions

Hi,

thanks to all for your excellent help!

BTW: Doppelte is a germany word. I'm sorry, but it was just one of my
first
sample projects,
but of course, normally all my codes are in english.
--
Regards
Thomas Neubauer
It (Doppelte) means Doubled if I recall correctly. My German is a bit
rusty though.

Nov 22 '07 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Jim McGrail | last post by:
Background: I am investigating a problem involving a windows .NET application that is being developed in C# with Visual Studio 2003. It is a multi-threaded application that uses MSMQ to...
2
by: Zalek Bloom | last post by:
Actually I would like to know what additional JCL I will need if a COBOL non-DB2 program is calling to a COBOL/DB2 program. The original non-DB2 program is executed using: //STEP1 EXEC ...
12
by: harishg2 | last post by:
Hi, How to store a variable value for more than one executions. Ex: main() { int i=0; i++; printf("%d",i);
8
by: Paul Bromley | last post by:
I am about to release the second version of a program that I started writing 12 months ago. The first version worked fine on 98 upwards. This version seems to crash on Windows 98. I realise that...
2
by: manuthomas23 | last post by:
Here is the code: #ifndef M3DCAMERA__H__ #define M3DCAMERA__H__ class M3DEngine; /** Micro3D Camera class. */
0
by: r1 | last post by:
I am relatively inexperienced in using delegates and asynchronous methods. I read several articles, and then I developed my own code with a mission to improve the performance. Wow! I cannot...
6
by: popone | last post by:
Hi All, I've developed a program that seems to crash randomly, from what I can tell it's after some user action. If it just sat there, it wouldn't crash. It's not an exception either, it's the...
41
by: z | last post by:
I use Visual C 2005 to develop my programs. One in particular is crashing in very specific and hard to replicate situations, made worse by the fact it only crashes when run -outside- the dev - as...
2
by: jaddle | last post by:
I program I'm writing crashes when it exits with a segfault. I'm runnning under linux. Using gdb, i see that the crash happens after the last line of main(), which is just a return(EXIT_SUCCESS); If...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.