2

I Have a string array with 5 values, i want the program to loop even though i don't enter a value for the arrays. If I split the arrays without inserting anything (pressing ..... (5 times "." to split the array) then it doesn't crash it will just loop. But if i just hit enter, then the program crashes.

Is there a way to fix the loop so that even though there is no kind of input, that it won't crash? (It also crashes if you don't complete all 5 values.)

Net = Console.ReadLine();
            string[] oktet = new string[5]; 
            oktet = Net.Split('.', '/'); 

            temp = oktet[0]; //inputs value of array in temp
            NaN = int.TryParse(temp, out Net0);
            temp = oktet[1];
            NaN = int.TryParse(temp, out Net1);
            temp = oktet[2];
            NaN = int.TryParse(temp, out Net2);
            temp = oktet[3];
            NaN = int.TryParse(temp, out Net3);
            temp = oktet[4];
            NaN = int.TryParse(temp, out subnet);
        }
        while (!NaN | Net0 > 255 | Net0 < 0 | Net1 > 255 | Net1 < 0 | Net2 > 255 | Net2 < 0 | Net3 > 255 | Net3 < 0 | subnet > 32 | subnet < 0);

I know it's pretty amateur, but hey, we're here to learn right? :)

Thanks in advanced!

Danny Varod
  • 17,324
  • 5
  • 69
  • 111
Nicholas
  • 783
  • 2
  • 7
  • 25
  • I Noticed it's missing a do{ but that's because i didn't get it in my copy/paste ^^ – Nicholas Mar 21 '12 at 17:00
  • Don't use system names such as NaN as variable names. Also local variable names should be camelCase and not PascalCase. Net is also not a good name, use ipV4Address instead. – Danny Varod Mar 21 '12 at 17:10
  • Another problem is that you are re-assigning NaN, so the test in while will only test if there was an error in extracting the value of subnet – Attila Mar 21 '12 at 17:13

4 Answers4

2

You can try to do something like this:

string[] oktet = Net.Split('.', '/'); // size array according to input

if (oktet.Length != 5) continue; // reloop on bad input

Those two lines of code replace these:

        string[] oktet = new string[5]; 
        oktet = Net.Split('.', '/'); 
Paul Sasik
  • 79,492
  • 20
  • 149
  • 189
0

I would just use some command line option library if you could (unless this is homework where you need to learn how to validate input, parse, etc.). See NDesk.Options (http://www.ndesk.org/Options), available via Nuget as well. See the following for required options: How to enforce required command-line options with NDesk.Options?

Community
  • 1
  • 1
Luke Hutton
  • 10,612
  • 6
  • 33
  • 58
0
var userInput = Console.ReadLine();

var userInputSplit = userInput.Split('.', '/');

var numbers = userInputSplit.Select(word =>
    {
        int result;
        if (byte.TryParse(word, out result))
            return (byte?)result;
        return (byte?)null; });
    });

var inputComplete = number.Where(number => number.HasValue).Count() == 4;
Danny Varod
  • 17,324
  • 5
  • 69
  • 111
0

The problem is that your variable oktet isn't an array of 5, because you're assigning something else in it.

  string[] oktet = new string[5]; // Assigns an array of 5
  oktet = Net.Split('.', '/'); //assigns the result of the split to the variable

So, oktet is has the result of the split, and its length.

BTW, Classes start with a capital letter, and the variables should start with a small letter.
Coding standards will make the code more readable, and will help you tell the difference between items.

Yochai Timmer
  • 48,127
  • 24
  • 147
  • 185