Deception Deception
Deception Deception

Reputation: 9

How to loop through an array that's been created from user input c#

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

Upvotes: 0

Views: 3082

Answers (2)

Hossain Muctadir
Hossain Muctadir

Reputation: 3626

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

Upvotes: 0

Steve
Steve

Reputation: 216293

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

Upvotes: 2

Related Questions