0

/*This is a simple code to add values to an array using c# and then just to print them. But I am having an error in order to do this. What possible error could be? */

        int size = 0, k = 0;
        Console.WriteLine("Enter size of array: ");            
        size = Console.Read();
        string[] array = new string[size];  

        for (int i = 0; i < size; i++)
        {
            Console.WriteLine("Enter at index: {0}", i);
            array[i] = Console.ReadLine();                
        }

        foreach (string s in array)
        {
            Console.WriteLine("Value at index: {0}", k++ + " = " + s);
        }
Amir
  • 1
  • 2

6 Answers6

2
k++

in the foreach loop would work.


I would actually just write:
Console.WriteLine("Value at index: {0} = {1}", k++, s);
Joe
  • 80,724
  • 18
  • 127
  • 145
  • yea thats right...i forgot to add k++ while pasting here...having yet same problem – Amir Feb 20 '13 at 22:27
  • What error are you getting? And can you fix your code above to show what is actually being run then? – jzworkman Feb 20 '13 at 22:28
  • Your first loop keeps running because your `Console.Read()` call is setting the size to the ascii value of the input instead of the actual number(so 49 for '1', or 52 for '4' in your case). You need to do a `Convert.ToUInt32(Colsole.ReadLine())` instead. – jzworkman Feb 20 '13 at 22:50
  • Exception problem after changing to [array[i] = Convert.ToUInt32(Colsole.ReadLine()).toString()]!...it says input string was not in correct format – Amir Feb 20 '13 at 22:54
  • size = Convert.ToInt32(Console.ReadLine()); NOW it worked....thanks allot....u r the hero :) – Amir Feb 20 '13 at 22:59
0

Twothings:

1)string[] array = new string[size]; likely to not compile, you can get a negative number

2) Console.WriteLine("Value at index: {0}", k + " = " + s); -> should be i think Console.WriteLine("Value at index: {0}" + k + " = " + s);

Ariel Pinchover
  • 654
  • 3
  • 17
0

I believe that you could achieve what you want by changing the code within your foreach loop as shown below:

Console.WriteLine("Value at index {0}: {1}", k++, s);

(looks like Joe already has this solution in his answer as well).

{0} is a placeholder for the value of the first parameter to the Console.WriteLine method after the format string parameter. {1} is a placeholder for the value of the second parameter to the Console.WriteLine method after the format string parameter.

I'm using k++ to increment the value of k on each iteration of the foreach loop.

A foreach loop simply iterates over an IEnumerable object (or more specifically, an IEnumerable<int> object in this case), and the functionality of an IEnumerable object is that it simply gives you each item in the collection. It has no concept of "index", so if you want to associate an index with each item, then you would need to take care of that yourself.

Note that for some collections, such as arrays and lists, you will always get the items in the order that they exist within those structures. However, the IEnumerable and IEnumerable<T> interfaces by themselves don't necessarily guarantee that you will get the items in any specific order, or that you will get the items in the same order if you loop over them a second time. The order of iteration is determined by the underlying implementation, which is an array of integers (int[]) in this case.

Dr. Wily's Apprentice
  • 10,212
  • 1
  • 25
  • 27
0

I think Console.Read() is causing you problems, try ReadLine() instead and convert to Int when necessary, and increment k:

 int size = 0, k = 0;
 Console.WriteLine("Enter size of array: ");
 size = Convert.ToInt32(Console.ReadLine());
 string[] array = new string[size];

 for (int i = 0; i < size; i++)
 {
     Console.WriteLine("Enter at index: {0}", i);
     array[i] = Console.ReadLine();
 }

 foreach (string s in array)
 {
     Console.WriteLine("Value at index: {0}", k + " = " + s);
     k++;
 }

 Console.ReadLine();
Colm Prunty
  • 1,595
  • 1
  • 11
  • 29
0

Why do you use Console.Read()? It reads integers corresponding to each of the characters in the input stream, but not until you press enter (which will in itself send two integers, one 13 and one 10).

Maybe you should use Console.ReadLine() instead?

Jeppe Stig Nielsen
  • 60,409
  • 11
  • 110
  • 181
  • yea ryt...but loop keeps going on – Amir Feb 20 '13 at 22:40
  • I mean only the first loop – Amir Feb 20 '13 at 22:47
  • @Amir You still have `Console.Read()` in `size = Console.Read();`. What `size` do you think that gives you? It depends on the codepoint of the first character read from the input stream. Do you want the user to type in a number instead? At least you should write out `size` so the user can see what size he gets. – Jeppe Stig Nielsen Feb 21 '13 at 09:52
0

This works (tested) :

        int size = 0, k = 0;
        Console.WriteLine("Enter size of array: ");
        size = int.Parse(Console.ReadLine());
        string[] array = new string[size];

        for (int i = 0; i < size; i++)
        {
            Console.WriteLine("Enter at index: {0}", i);
            array[i] =  Console.ReadLine();
        }

        foreach (string s in array)
        {
            Console.WriteLine("Value at index: {0} = {1}", k++, s);
        }

        Console.Read();
Abdusalam Ben Haj
  • 5,343
  • 5
  • 31
  • 45