I have a console application with two classes:
- MyIntList Which initilzes the array and handles the input and outputs.
- Program This has the Main() method which handles the users input and sends them too the handlers in MyIntList.

The problem I am having is that when the array is output and shown in the console, the values are completly different. For example here is my method for handling the input:

class Program
    {
        static void Main(string[] args)
        {
            int input;
            MyIntList a1 = new MyIntList();
            Console.WriteLine("Enter a number to fill the array");
            for (int i = 0; i < 5; i++)
            {
                input = Console.Read();         
                a1.Add(input);
                Console.ReadLine();
            }
            a1.show();       
        }
    }
}

I type in 5 lots of 1's;

These values get sent to the array handlers:

class MyIntList
    {
        private int[] intArray;
        private int count;
        public MyIntList()
        {
            intArray = new int[5];
            count = 0;
        }

        public void Add(int value)
        {
            if(count >=5)
            {
                return;
            }
            intArray[count] = value;
            count++;
        }

        public void show()
        {
            for(int i = 0; i < 5; i++)
            {
                Console.WriteLine(intArray[i]);
            }
            Console.ReadLine();
        }
    }
}

The values I get are:
49
49
49
49
49

When I have entered in 1's.

as you can see Add() saftey checks if the user has entered over 5 values if not then count is incremented.
obviously show() loops through until the length of IntArray has been reached and shows each line.

I cant seem to figure out why it outputs different values from what has been entered.

The preferred method is ReadLine instead of Read. ReadLine returns a string, so you have to do a conversion to integer. Use the TryParse method for that.

Console.Read() accepts the first character of the string and returns ASCII code (Integer Value) of the first character.

Hence as ddanbe suggested, use Console.ReadLine(), this takes a string and returns a string.(and yea, dont forget to typecast)

and thats y u get 49 since the ASCII value for 1 is 49.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.