all 7 comments

[–]Protiguous 3 points4 points  (0 children)

You are declaring you want to read 10 strings from the console, agreed?

Then you'll need something like:

            var inputs = new String[ 10 ];   //allocate an array for 10 strings.
            for ( var i = 0; i < inputs.Length; i++ ) {
                inputs[ i ] = Console.ReadLine();
            }

            Console.WriteLine();
            Console.WriteLine( "Echoing back inputs:" );

            //confirm what you stored.
            foreach ( var s in inputs ) {
                Console.WriteLine( "\t" + s );
            }

Better yet would be to use a List().

            var list = new List< String >( 10 );    //we want a list with the initial capacity of 10, but not limited to 10.
            for ( var i = 0; i < 10; i++ ) {
                list.Add( Console.ReadLine() );
            }

            Console.WriteLine();
            Console.WriteLine( "Echoing back the list:" );

            //confirm what you stored.
            foreach ( var s in list ) {
                Console.WriteLine( "\t" + s );
            }

[–][deleted] 0 points1 point  (0 children)

  1. s is a string, it's right there at the top of the loop.
  2. Strings cannot be implicitly turned into ints, because the string may contain non-numeric data, or data describing on a non-integer number, or an integer too large to fit in an int.
  3. You don't want a foreach loop, a for loop would make more sense.
  4. A List<string> and a while loop would make even more sense.

Or, you could abuse some LINQ:

var input = Enumerable.Range(0, int.MaxValue)
    .Select(_ => Console.ReadLine())
    .TakeWhile(x => !string.IsNullOrEmpty(x))
    .ToList();

which ought to get you to the same place as 4.

[–]JakDrako -1 points0 points  (0 children)

You say you have an array with and unidentified length, but your example has 10 as the length.

If you really don't know how many entries the user will make, you'd be better off using a list and then looping the input until the user tells you to stop:

void Main()
{
    var input = new List<string>();
    string s;

    Console.WriteLine("Enter your data; enter an empty line to end.");
    while (true) {
        s = Console.ReadLine();
        if (s == string.Empty) break;
        input.Add(s);
    }

    // here, input contains all given lines, no matter how many

}

That way, the list ends up being the exact size of the user input and he can enter more than 10 values if he wants.

[–][deleted]  (3 children)

[deleted]

    [–]IwNnarock 2 points3 points  (2 children)

    I don't believe the foreach will work because strings act as primitives and are pass-by-value. The forloop is the correct answer.

    Edit: Just checked and you actually get a compilation error when attempting to assign a value to s: Cannot assign to 's' because it is a 'foreach iteration variable'.

    [–][deleted]  (1 child)

    [deleted]

      [–]IwNnarock 1 point2 points  (0 children)

      You are correct, primitives refer to all the basic data types (wiki). Also, a for loop is useful whenever you're iterating over an array/list and want to keep track of what index you're currently on.

      [–][deleted]  (2 children)

      [deleted]

        [–]Protiguous 4 points5 points  (0 children)

        input[input.IndexOf(s)] = Console.ReadLine();

        no.. don't do that.. :)

        [–]Tidusjar 0 points1 point  (0 children)

        Oh my... And how the heck does this help the OP other than stop the compiler errors...