0

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.

3
Contributors
4
Replies
33
Views
2 Years
Discussion Span
Last Post by iConqueror
Featured Replies
0

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)

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.