473,654 Members | 3,280 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Collecti ons.Generic;
using System.Componen tModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows. Forms;

namespace WindowsApplicat ion1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeCompo nent();
}

private void btZieheLottozah len_Click(objec t 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.Nex t(1, 64);
}
Array.Sort(Zahl en);
for (int i = 0; i < 5; i++)
if (Zahlen[i] == Zahlen[i + 1])
Doppelte = 1;

} while (Doppelte == 1);

label1.Text = Convert.ToStrin g(Zahlen[0]);
label2.Text = Convert.ToStrin g(Zahlen[1]);
label3.Text = Convert.ToStrin g(Zahlen[2]);
label4.Text = Convert.ToStrin g(Zahlen[3]);
label5.Text = Convert.ToStrin g(Zahlen[4]);
label6.Text = Convert.ToStrin g(Zahlen[5]);
}
}
}
Nov 20 '07 #1
13 1622
Thomas Neubauer <ng****@vol.atw rote:
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.co m>
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.ats aid:
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.ats aid:
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.ats aid:
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.mi crosoft.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.Nex t(1, 64);
}
Array.Sort(Zahl en);
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.ats aid:
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(newNu mber);
}
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

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

Similar topics

2
2084
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 communicate between the threads. The problem is that the program infrequently terminates with no indication of why it terminated.
2
22435
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 PGM=PROG1 I know I have to supply database name/plan to my program, but how? Another question: what happens is the PROG1 program is a DB2 program and is calling to another DB2 program with a different database and a different plan?
12
2262
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
1345
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 this is a bit vague, but anyone any broad guidance as to how I might go about sorting this one?? To make matters worse, It seems to work fine on a Windows 98 machine home - loaded onto 2 at this other site with dotnet framework and it crashes. The...
2
2714
by: manuthomas23 | last post by:
Here is the code: #ifndef M3DCAMERA__H__ #define M3DCAMERA__H__ class M3DEngine; /** Micro3D Camera class. */
0
1270
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 believe the difference in speed! However, the asynch operation fails sometimes, despite of the fact that it works most of the time. I am really at a loss how to fix this sporadic and erratic behavior. This is a web application developed with...
6
1593
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 error message that asks if you want to send a report to microsoft. I have no clue where to start hunting for the problem. I only use Interop to hide the start menu, i don't think that is causing the problem. Also, on the development machine,...
41
2444
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 an exe, not from the Debug option. If I try to launch the debug on the crashing program, it'll close before the debugger opens. Any suggestions as to how I should proceed? Thanks in advance.
2
6421
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 I put an exit(EXIT_SUCCESS); right before that line, it doesn't crash though! In gdb, when the crash occurs, a backtrace gives the following: #0 0x0804e090 in ?? () #1 0xb7ea2ff4 in ?? () from /usr/lib/debug/libc.so.6 #2 0xb7ea4140 in ?? ()...
0
8815
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8708
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7307
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6161
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5622
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4294
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2716
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1916
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1596
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.