how to add elements to an array based on a condition in C#? -


i'm working on simple c# program adding elements array. allow user enter 5 numbers, , if user enters invalid valid have message that. issue whether users enters invalid number or not still want add 5 numbers array. code works, lets user enters 3 numbers , 2 words , end having 3 numbers, want 5 numbers no matter what. please me finding i'm doing wrong. thank in advance.

here's code:

        int[] numbers = new int[5];          (int = 0; < 5; i++)         {             console.writeline("enter number: ");             string c = console.readline();             int value;             if (int.tryparse(c, out value))             {                 numbers[i] = value;             }             else             {                 console.writeline("you did not enter number\n");             }         }          (int = 0; < numbers.length; i++ )         {             console.write(numbers[i] + " ");         } 

you can reduced increment count 1, when user inputs wrong/no number.

also note, code reading input 4(not 5 question description says.) numbers.

int[] numbers = new int[4];  (int = 0; < 4; i++) {     console.writeline("enter number: ");     string c = console.readline();     int value;     if (int.tryparse(c, out value))     {         numbers[i] = value;     }     else     {         i--;         console.writeline("you did not enter number\n");     } }  (int = 0; < numbers.length; i++ ) {     console.write(numbers[i] + " "); } 

Comments

Popular posts from this blog

Delphi XE2 Indy10 udp client-server interchange using SendBuffer-ReceiveBuffer -

Qt ActiveX WMI QAxBase::dynamicCallHelper: ItemIndex(int): No such property in -

Enable autocomplete or intellisense in Atom editor for PHP -