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();
}
}
}