0

I'm trying to do an assignment for my coding class and I'm quite new to this. My class wants someone to input 5 values into an array from user input. The statements MUST be in a while loop.

Here's my code

static void Main(string[] args)
        {
            string[] numbers = new string[6];
            int i = 1;
            while (i <= 5)
            {
                Console.Write("Please enter a number here:");
                numbers[i] = Convert.ToInt32(Console.ReadLine());
                i++;
            }

        

        }
    }
}

The error occurs at

numbers[i] = Convert.ToInt32(Console.ReadLine());

I'm trying to convert the user input into an int value, but It doesn't let me. Any reason why? please let me know and understand I'm really new to this.

4
  • 4
    You have declared an array of strings then you try to put into that array the result of the conversion of the Console.ReadLine (a string) to an int. Of course C# (a strongly typed language) doesn't allow you to do that. Declare your array as int not as strings. Consider also that if your user types something that is not a number Convert.ToInt32 crashes. You should use Int32.TryParse for conversion of user inputted numbers Commented May 2, 2021 at 10:22
  • 1
    Looks like you got the same (entering 5 values and converting) homework assignment? ;) Commented May 2, 2021 at 10:26
  • @MickyD How curious; same task and the same type of problem (wrong types). Commented May 2, 2021 at 12:01
  • @MickyD yup haha Commented May 2, 2021 at 21:46

2 Answers 2

2

You are trying to convert an input which data type is a string to an int data type. Change the numbers array data type from string to int.

int[] numbers = new int[6];

I would also add a check to see if the input from the user is indeed a number via Int32.TryParse.

This function will try to Convert the string (first argument) to a number in its 32-bit signed integer representation and returns a boolean value which indicates whether the conversion succeeded.

 string value = Console.ReadLine();
 int number;
 bool success = Int32.TryParse(value, out number);
 if (success)
 {
    numbers.Add(number);
 }
Sign up to request clarification or add additional context in comments.

2 Comments

Such an obvious thing, but I didn't think it haha. Thank you
No problem @H0tline (: Can you please accept my answer?
0

It's because the Data type of your array is on type string and on your user input, you are trying to convert it as an int so you will definitely have an exception

Since you are using "Please enter a number here:" on your ouput, you can make the type of your array as an int

  int[] numbers = new int[6];
  int i = 1;
  while (i <= 5)
  {
      Console.Write("Please enter a number here:");
      numbers[i] = Convert.ToInt32(Console.ReadLine());
      i++;
  }

1 Comment

If you really want to help then you should also explain how to solve the problem for user inserting a letter instead of a number.

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.