I don't understand what I am doing wrong. Can anyone help me please???

Code blocks are created by indenting at least 4 spaces
... and can span multiple lines

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace chapter_5_c_sharp_array
{
class Program
{
static void Main(string[] args)
{
int x;

        Console.Write("How many people are there in your room? ");
        x = Convert.ToInt32(Console.ReadLine());
        Console.WriteLine(" ");

        for (int i = 0; i < x; i++)
        {
            string[] names_ = new string[i];
            Console.Write("Enter a name: ");
            names_[i] = Console.ReadLine();
            Console.WriteLine(names_[i]);
        }

     }
}

}

Recommended Answers

All 5 Replies

Remove Line 7.
Insert this:

string[] names_ = new string[x];

before 5th line.

You might also want to put your printing loop outside of your "gathering" loop, so you can display the names all at the same time.

YOu create a new instacne of array on each stop while going through for loop. This is sure something you dont want to.
Do as skaa said:

    Console.Write("How many people are there in your room? ");
    x = Convert.ToInt32(Console.ReadLine());
    Console.WriteLine(" ");
    string[] names_ = new string[x];
    for (int i = 0; i < names_.Lenght; i++)
    {            
        Console.Write("Enter a name: ");
        names_[i] = Console.ReadLine();
        Console.WriteLine("You entered: " + names_[i]);
    }

Thank You guys =) Another question though, how come I can't Console.WriteLine(names_[i]); outside of the for loop???? How can I bring out the i in there? At the end of the for loop would I do something like return i; ??????

The variable i determines the offset into the array of what will be written.
To issue taht command without the i, just put in a number of the element you want printed.

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.