2

I want to let the user define the size of the array and all of the numbers inside it, so this is what I had in mind:

int []arr={0};
Console.WriteLine("Please enter the size of the array:");
size = Console.Read();

for(int i=0; i<size; i++)
{
    Console.WriteLine("Please enter the next number in the array, it's position is: " + i);
    arr[i] = Console.Read();
}

When I try to run it gives me an 'index out of range' error. If someone could point what am I doing wrong I would really appreciate it.

EDIT: After the answer I changed the code a little bit and now it looks like this:

Console.WriteLine("Please enter the size of the array:");
input = Console.ReadLine();
size = int.Parse(input);
int[] arr = new int[size];

for(int i=0; i<size; i++)
{
    string b;
    Console.WriteLine("Please enter the next number in the array, it's position is: " + i);
    b = Console.ReadLine();
    arr[i] = int.Parse(b); 
}

So now the array can be bigger and so does the numbers inside, thanks again for the help!

1
  • The first line of your code creates an array with the length of 1. Your size variable is not used to initialize the array. Commented Jan 27, 2016 at 20:03

3 Answers 3

3

You need to initialize the array after getting input from user:

Console.WriteLine("Please enter the size of the array:");
int size = int.Parse(Console.ReadLine()); //you need to parse the input too
int[] arr = new int[size];

for(int i=0; i < arr.Length; i++)
{
    Console.WriteLine("Please enter the next number in the array, it's position is: " + i);
    arr[i] = int.Parse(Console.ReadLine()); //parsing to "int"
}

Note: You should not use Console.Read() it returns an ASCII of the character as Szabolcs Dézsi mentioned in the comments. You should use Console.ReadLine() instead.

Sign up to request clarification or add additional context in comments.

3 Comments

Console.Read returns int, so you don't need to parse it. And I wouldn't recommend it to read in the size of the array, it returns the ASCII code of the character.
Thanks for the info, will correct the answer for sure.
Hey man thank for the help, I reworked the code and now it's all working.
0

You need to re-instantiate your array object:

int []arr={0};
Console.WriteLine("Please enter the size of the array:");
size = Console.Read();
arr=new int[size];
for(int i=0; i<size; i++){
   Console.WriteLine("Please enter the next number in the array, it's position is: " + i);
   arr[i] = Console.Read();
            }

Comments

0

Still not the most ideal, but this will prevent the user from entering non-numeric data. Keeps prompting each time until the data is valid.

Console.WriteLine("Please enter the size of the array:");
int size;

while(!int.TryParse(Console.ReadLine(), out size))
{
    Console.WriteLine("Please enter the size of the array:");
}

var arr = new int[size];

for (int i = 0; i < size; i++)
{
    Console.WriteLine("Please enter the next number in the array, it's position is: " + i);
    int currentElement;
    while (!int.TryParse(Console.ReadLine(), out currentElement))
    {
        Console.WriteLine("Please enter the next number in the array, it's position is: " + i);
    }

    arr[i] = currentElement;
}

I would not use Console.Read, because with that you can only read one character at a time. So at most you can only read 0-9 for the array size, and if you enter lets say 15, the length of the array will be 1, and the next Read will read 5 and fill in the first element.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.