[.net] Is this program working correctly?

Started by
3 comments, last by benryves 17 years, 6 months ago
If anyone spots any errors in this program, can they please let me know ASAP? I know there aren't any syntactical errors, but if there are any runtime errors, it is critical for me to know ASAP. I wouldn't normally make a post like this, but the reason I'm doing it now is because the program is causing the computer to make an awful lot of noise due to some ASCII characters I'm printing out, and as such I can't really use the computer now, so I'd really like to turn the program off if it isn't going to work correctly anyhow. I don't need to know the reason it's making sounds, as I already know that, I just need to know if the program is working correctly. Thanks! Here's the source:
using System;
using System.Collections.Generic;
using System.Text;

namespace Codemonkey_test
{
    class Program
    {
        static void Main(string[] args)
        {
            GenerateFunction();
        }

        static void GenerateFunction()
        {
            char[] ChrArray = new char[30];
            int[] ASCII = new int[30];
            bool Exit = false;

            Random Rnd = new Random();

            while (!Exit)
            {
            Start:
                for (int i = 0; i < ChrArray.Length; i++)
                {
                    ASCII = Rnd.Next(1, 255);
                    ChrArray = (char)ASCII;
                }

                string Str = new String(ChrArray);
                Console.WriteLine(Str);

                if (Str.Equals("Potato getPotato(){return potato;}", StringComparison.CurrentCultureIgnoreCase))
                    Exit = true;
                else
                    goto Start;
            }
        }
    }
}

Please look at my last post in this thread for a reference as to why I created this thread and the program.
_______________________Afr0Games
Advertisement
First of all, gotos are the root of all evil and you definately do not need it in what you're doing right now. If you remove "Start:" and the else statement, then it should be fine.

The unfortunate thing are the chances of ever getting "Potato getPotato(){return potato;}" from a random generation of characters. The loop will most likely never break, because it'll almost never randomly generate that string.
Rob Loach [Website] [Projects] [Contact]
Thanks! :)

And yes, I'm beginning to think you're right about the last part. As of now the program's been running for over 15 hours and still hasn't produced any result. :\
_______________________Afr0Games
if you change your random range to 32,255 that way you won't be trying to print control characters, of which BEEP is sub 32 iirc.
Quote:Original post by Niksan
if you change your random range to 32,255 that way you won't be trying to print control characters, of which BEEP is sub 32 iirc.
If doing that, you might as well limit to ASCII and use 32, 127.
Quote:Original post by Rob Loach
First of all, gotos are the root of all evil...
Complete and utter nonsense. That said, I agree that there is no need for a goto there.

I'd prefer to do it like this:

for ( ; ;) { // while (true) is also a possibility    // Build up string...    if (Str.Equals("Potato getPotato(){return potato;}", StringComparison.CurrentCultureIgnoreCase)) {        break;    }}

[Website] [+++ Divide By Cucumber Error. Please Reinstall Universe And Reboot +++]

This topic is closed to new replies.

Advertisement