0

Well I'm trying to make a simple program that takes advantage of a for loop and adds user input to an array one at a time, this uses this

string []str = new string[10];
for (int i = 0; i < str.Length; i++)
{
     Console.WriteLine("Please enter a number: ");
     str[i] = Console.ReadLine();
}

But When i try and loop through the array with a foreach statement, i get an error stating that i can't implicitly convert string[] to type String; the foreach statement is this:

int even=0; int odd=0;

int[] Arr=new string [] {str};

foreach (int i in Arr)
{
    if (i % 2 == 0)
    {
        even++;
    }
    else
    {
        odd++;
    }
}

And here is the full source,

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] str = new string[10];
            for (int i = 0; i < str.Length; i++)
            {
                Console.WriteLine("Please enter a number: ");
                str[i] = Console.ReadLine();
            }
            int even = 0; int odd = 0;
            int[] Arr = new string[] { str };
            foreach (int i in Arr)
            {
                if (i % 2 == 0)
                {
                    even++;
                }
                else
                {
                    odd++;
                }
            }
            Console.WriteLine("There is " + even + " even numbers.");
            Console.WriteLine("There is " + odd + " odd numbers");
            Console.ReadLine();
            Console.ReadLine();
        }
    }
}
1
  • Because you create an integer array and try to fill/initialize it with strings. Check your second line of code in the second codebox Commented Oct 20, 2013 at 20:33

2 Answers 2

2

Change your input code to save the user input directly in an array of integers instead of strings

    int i = 0;
    int[]values = new int[10];
    while(i < values.Length)
    {
        Console.WriteLine("Please enter a number: ");
        int result;
        string input = Console.ReadLine();
        if(Int32.TryParse(input, out result)
        {
            values[i] = result;
            i++;
        }
        else
        { 
            Console.WriteLine("Not a valid integer");
        }
    }

This will avoid the error when in this line int[] Arr=new string [] {str}; you try to initialize an array of integers from an array of strings and the compiler is not happy with it

Apart from the obvious compilation error, using Int32.TryParse allows to check immediately if the user types something that is not an integer and you can deny the input

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

2 Comments

Small nitpicking: str itself is a string[]. So, he actually tried to add a string[] as element to a string[]... Spinning heads... :)
Fine, but you have accepted another one, so this is not really good for you
0

In the line below you are trying to create an integer array from all your input. But actually this kind of syntax is not correct. Firstly you are trying to create an array of int out of array of string. This is not possible. Secondly a string array is created like new string[]{"str", "str"} but you are doing new string[]{str[]}. So to solve all these issues i recommend replace

int[] Arr=new string [] {str};

with

int[] Arr = str.Select(s => int.Parse(s)).ToArray();

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.