Click here to Skip to main content
15,892,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to make a simple little program that adds strings to an array then print all the strings. Turns out to be anything but simple because I can't even seem to add the strings to the array! Every time I try this it compiles but crashes the moment I give any input.

What I have tried:

C#
using System;
public class Practice
{

    static void Main()
    {
        int x=0; //num of words
        string[] str = new string[x];
        string ec = "not q"; //exit clause
        for(x=0; ec != "q"; x++)
        {
            str[x] = Console.ReadLine();
            ec = Console.ReadLine();

        }
    }

    

}
Posted
Updated 5-Apr-17 1:34am
Comments
Karthik_Mahalingam 5-Apr-17 7:16am    
what is your exact requirement and the issue you are facing?
your question and the code seems different.
Snowy_Panther 5-Apr-17 7:17am    
I am trying at add strings to an array using console input. I haven't gotten to the print out of the array yet.
Karthik_Mahalingam 5-Apr-17 7:24am    
refer the below example.
Snowy_Panther 5-Apr-17 7:55am    
Okay so this is what I came up with:

<pre>using System;public class Practice{     static void Main()    {         int a = int.Parse(Console.ReadLine()); //amount of words        string[] array = new string[a];        for(int b=0;b<a; b++)        {            array[b] = Console.ReadLine();        }        Console.WriteLine("Press any key to continue...");        Console.ReadKey();        for(int b=0; b< array.Length; b++)        {            Console.WriteLine(array[b]);        }        Console.ReadKey();      }


Still have a question. In my case, if you enter a number of strings you want, you get that number of strings. Shouldn't it be one more since C# starts counting at 0?

Anyway thanks for the help guys, I really appreciate the fast responses.

In C# when you declare an array , you have to specify the size; In the above code you have to initialize your array to some size .

string[] str = new string[10];


When you init an array with size zero ,it will create an empty array object .

Here I am initializing the array to be of size 10 .And in the for loop , may be you can check for the array length and based on the check you can declare a new array of the desired size and copy the contents of the original array into the new array.

Again there are build in methods like Array.Resize .
Array.Resize(T) Method (T[], Int32) (System)[^]
 
Share this answer
 
v4
Quote:
I am trying at add strings to an array using console input. I haven't gotten to the print out of the array yet.

refer this simple example

System.Console.WriteLine("Enter 5 words");
       string[] array = new string[5];
       for (int i = 0; i < 5; i++)
       {
           array[i] = System.Console.ReadLine(); // read the line and stores in array
       }
       System.Console.WriteLine("press any key to print words");
       System.Console.ReadLine();
       foreach (string item in array)
       {
           System.Console.WriteLine(item);  // print each item from the array
       }
       System.Console.ReadLine();
 
Share this answer
 
v2
Why don't you use a List, instead?
E.g.
C#
public static void Main()
{
  List<string> strlist = new List<string>();
  string str;
  while (true)
  {
    Console.WriteLine("please enter a string ('q' to exit");
    str = Console.ReadLine();
    if (str == "q") break;
    strlist.Add(str);
  }

  Console.WriteLine("Entered string count = {0}", strlist.Count);
  foreach (string s in strlist)
  {
    Console.WriteLine(s);
  }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900