You must Sign In to post a response.
  • Category: Visual Studio

    Save new input into a previous existing Array

    I'm stuck on how to change the data within an array. I had to use the data within the array 'word' for a previous portion of my code however I need to change the input and I'm trying to do it within a for loop. The first for loop needs to ask the user for their third favorite movie that's why its 3-i. My code works below however I don't know how to store what the user types in into the previous array input. In the second for loop I need to display the list of movies that they entered in the first for loop. Any suggestions? FYI I know my second for loop is incorrect I just don't know how to store the data properly into the array ' words' in order to redisplay the data. Please help.

    string[] words = new string[3];
    words[0] = "green";
    words[1] = "blue";
    words[2] = "red";

    for (int i = 0; i < words.Length; i++ )
    {
    Console.Write("Favorite Movie #{0} : ", 3-i);
    }
    for (int i = 0; i < words.Length; i++ )
    //{
    // Console.Write("#{3} - " + words[0] + "#{2}" - + words[1] + "#{3}" + words[2]);
    //}
  • #752446
    Hai Christina,
    You can use something like below:

    string[] words = new string[3];
    words[0] = "green";
    words[1] = "blue";
    words[2] = "red";
    for (int i = 0; i < words.Length; i++)
    {
    Console.Write("Favorite Movie # {0} : ", i);
    }
    for (int i = 0; i < words.Length; i++)
    {
    Console.Write("# {0} - " +i, words[i]);
    }

    Hope it will be helpful to you.

    Regards,
    Pawan Awasthi(DNS MVM)
    +91 8123489140 (whatsApp), +60 14365 1476(Malaysia)
    pawansoftit@gmail.com

  • #752449
    Hello Christina,

    What you can do is that in your first loop only, Print message for user and ask him to enter value. Read that value and store in array. This loop continues till user enters all values.

    Then in second Loop you can print his selection.

    For Ex:-


    string[] words = new string[3];

    for (int i = 0; i < words.Length; i++)
    {
    Console.WriteLine("Favorite Movie # {0} : ", i+1);//To print Message
    words[i] = Console.ReadLine();//To Read Input and store
    }

    for (int i = 0; i < words.Length; i++)
    {
    Console.WriteLine("Favorite Movie # {0} ", (i+1) + " " + words[i]);//To Display
    }

    Console.ReadLine();


    Have a look at attached output

    Regards,
    Shakil Sama

    Delete Attachment

  • #752458
    Thank you Shakil Sama that was exactly what I was missing!

  • #752518
    Welcome. If you find solution appropriate then do "Mark as best answer".
    Regards,
    Shakil Sama


  • Sign In to post your comments