I have a snippet of code and for some odd reason I can not print it.

 int[] answer = new int[15];
            for (int i = 0; i < answer.Length; i++)
            {
                answer[i] = Console.ReadLine(answer);
            }

It says that I can not do Console.ReadLine(answer);. How would I get this snippet of code to work?

Recommended Answers

All 17 Replies

ReadLine is for reading input not writing to the output. You need WriteLine for that, Console.WriteLine(answer[i]);

okay, so I tried that and my code looks like this now:

   int[] answer = new int[5];
            for (int i = 0; i < answer.Length; i++)
            {
                answer[i] = Console.WriteLine(answer[i]);
            }

It still doesn't work, I still get an error. Did I do it wrong?

According to the manual WriteLine() returns void, you can't assign that to answer[i].

okay, so if that is the case, what would I have to do to get it to work?

 int[] answer = new int[15];
            for (int i = 0; i < answer.Length; i++)
            {
                Console.WriteLine(answer[i]);
            }
            Console.ReadLine();

but you didn t specify any values in the array so you ll get zeros here.

commented: Thanks for helping me! +0

So what if I were to have an array and have the users populate it?

static void Main(string[] args)
        {
            int[] answer = new int[15];
            for (int i = 0; i < answer.Length; i++) 
            {
                answer[i] = Convert.ToInt32(Console.ReadLine()); // if string remove convert.toint32
            }
            Console.WriteLine("output is :");
            for (int i = 0; i < answer.Length; i++) 
            {
                Console.WriteLine(answer[i]);
            }
            Console.ReadLine();
        }
commented: Thanks for helping me! +7

Quick question, I have tried using your code for strings. It didn't work and I couldn't get it to work with Doubles either.

I switched the int into strings/double.

Strings:

        static void Main(string[] args)
        {
            string[] answer = new string[15];
            for (int i = 0; i < answer.Length; i++)
            {
                answer[i] = Console.ReadLine();
            }
            Console.WriteLine("output is :");
            for (int i = 0; i < answer.Length; i++)
            {
                Console.WriteLine(answer[i]);
            }
            Console.ReadLine();
        }

Doubles:

        static void Main(string[] args)
        {
            double[] answer = new double[15];
            for (int i = 0; i < answer.Length; i++)
            {
                answer[i] =Convert.ToDouble(Console.ReadLine());
            }
            Console.WriteLine("output is :");
            for (int i = 0; i < answer.Length; i++)
            {
                Console.WriteLine(answer[i]);
            }
            Console.ReadLine();
        }

Oh... so I see where I was making my mistakes. I was making my mistakes on line 6. I kept it similar to the one for integers.

Okay so for the double one, I tried to make a custom array size, sorry it was the best that I can call it. Basically what I am trying to do for the double array is to make the size of whatever the user wants.

So I tried to collect the size and it doesn't work.

 int dNumber = 0.0;
            Console.WriteLine("Type a number.");
            line = Console.ReadLine();
            dNumber = int.Parse(line);
            int.TryParse(line, out dNumber);

I was using the above to collect the number to change the size of the array ( I hope I didn't paste the wrong one ).

Also, how do I apply an if statement to the string one, so in any case, if I input the word dog, the program would stop. I am currently beating myself on this one.

Sorry for the many questions, the last 2 arrays are not homework (the integer one was), I am just learning some techniques with arrays. But thanks for helping me as of so far.

To get a int, you can try something like this

int myNumber;
String input;
do {
    Console.Write("Enter a number : ");
    input = Console.ReadLine();
} while (Int32.TryParse(input, out myNumber) == false);

This will prompt the user over and over again for a number (integer number) until they enter one.

commented: Very nice code snippet! +14

okay so how do combine that into this code:

        static void Main(string[] args)
        {
            double[] answer = new double[15];
            for (int i = 0; i < answer.Length; i++)
            {
                answer[i] =Convert.ToDouble(Console.ReadLine());
            }
            Console.WriteLine("output is :");
            for (int i = 0; i < answer.Length; i++)
            {
                Console.WriteLine(answer[i]);
            }
            Console.ReadLine();
        }

I got an error from trying to combine your source. for some odd reason, VS gave me an error saying that I can't change answer.Length into input.

I tried to end a program by using this, it doesn't work

 double[] answer = new double[15];
            for (int b = 0; b < answer.Length; b++)
            {
                answer[b] = Convert.ToDouble(Console.ReadLine());
            }
            Console.WriteLine("Inputs: ");
            for (int b = 0; b < answer.Length; b++)
            {
                Console.WriteLine(answer[b]);
                if (b = "stop")
                {
                    Environment.Exit();
                }
            }
            Console.ReadLine();

try double[] answer = new double[myNumber];

Is this what you are refering to:

 int myNumber;
            String input;
            do
            {
                Console.Write("Enter a number : ");
                input = Console.ReadLine();
            } 
            while (Int32.TryParse(input, out myNumber) == false);

            double[] answer = new double[mynumber]; 
            for (int i = 0; i < answer.Length; i++)
            {
                answer[i] = Convert.ToDouble(Console.ReadLine());
            }
            Console.WriteLine("output is :");
            for (int i = 0; i < answer.Length; i++)
            {
                Console.WriteLine(answer[i]);
            }
            Console.ReadLine();

I still can't collect numbers. It seems to skip the entire code until it reaches line 10.

Something like this should work:

        int myNumber;
        String input;
        do
        {
            Console.Write("Enter a number : ");
            input = Console.ReadLine();
        } while (Int32.TryParse(input, out myNumber) == false);

        double[] answer = new double[myNumber];
        for (int b = 0; b < answer.Length; b++)
        {
            do
            {
                input = Console.ReadLine();
                if (input == "q")
                    Environment.Exit(0);
            } while (Double.TryParse(input, out answer[b]) == false);
        }
        Console.WriteLine("Inputs: ");
        for (int b = 0; b < answer.Length; b++)
        {
            Console.WriteLine(answer[b]);
        }
        Console.ReadLine();
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.