0

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?

5
Contributors
17
Replies
69
Views
4 Years
Discussion Span
Last Post by tinstaafl
Featured Replies
  • `ReadLine` is for reading input not writing to the output. You need `WriteLine` for that, `Console.WriteLine(answer[i]);` Read More

  • 1

    According to [the manual](http://msdn.microsoft.com/en-us/library/70x4wcx1.aspx) WriteLine() returns void, you can't assign that to `answer[i]`. Read More

  • 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. Read More

  • 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(); } Read More

  • 1

    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. Read More

1

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

0

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?

0

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

1
 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.

Votes + Comments
Thanks for helping me!
0

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

1
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();
        }
Votes + Comments
Thanks for helping me!
0

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.

0

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();
        }

Edited by tinstaafl

0

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.

0

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.

1

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.

Edited by Momerath

Votes + Comments
Very nice code snippet!
0

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.

0

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();
0

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

Edited by tinstaafl: Sorry did a bit of a re-write

0

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.

0

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();

Edited by tinstaafl

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.