'dont understand how to change my loop array - c#

Beginner programmer here. I've written an app for an assignment that asks for the number of people in a competition in two consecutive years, then asks for their names, a code for their respective talents (S = singing, D = dancing, M = musical instruments, and O = other), then allows you to enter the code and provide a list of names within each code section, before providing a sentinal value to exit. My issue is I want my array loop (sorry if terminology wrong I'm a beginner week 2) to complete the 10 names with 10 codes before displaying the list with each section. Currently it asks for name, then code, then displays the list of each talent code, then allows you to enter a code to see a list of people in that section, then when prompted with the sentinal value, it completeles the loop and asks for contestant twos name. Sorry if that doesnt make sense im still trying to figure out this whole coding thing. If anyone could explain in lamens terms that would be great. Thanks.

using System;
using static System.Console;

namespace QueenslandRevenue
{
    class Program
    {
        static void Main(string[] args)
        {
            int yearOneContestants, yearTwoContestants;
            WriteLine("What was the number of contestants that entered into last year's competition?");
            yearOneContestants = Convert.ToInt32(ReadLine());
            WriteLine("What was the number of contestants that entered into this year's competition?");
            yearTwoContestants = Convert.ToInt32(ReadLine());

            while (yearOneContestants > 30 || yearTwoContestants > 30)
            {
                Console.WriteLine("Contestant numbers are incorrect");
                Console.WriteLine("What was the number of contestants that entered into last year's competition?");
                yearOneContestants = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("What was the number of contestants that entered into this year's competition?");
                yearTwoContestants = Convert.ToInt32(Console.ReadLine());
            }

            Console.WriteLine("The number of people in last years competition were {0}, the number of people in this years competition were {1}.", yearOneContestants, yearTwoContestants);

            if (yearTwoContestants > (yearOneContestants * 2))

                Console.WriteLine("The competition is more than twice as big this year!");

            else if (yearTwoContestants >= yearOneContestants && yearTwoContestants < (yearOneContestants * 2))


                Console.WriteLine("The competition is bigger than ever!");


            else if (yearTwoContestants < yearOneContestants)

                Console.WriteLine("A tighter race this year! Come out and cast your vote!");

            string[] contestantNames = new string[yearTwoContestants];
            string[] contestantCode = new string[yearTwoContestants];
            for (int x = 0; x < yearTwoContestants; x++)
            {
                Console.WriteLine("Please enter contestant {0}'s name ", (x + 1));
                contestantNames[x] = Console.ReadLine();

                bool correct = false;
                while (!correct)

                {
                    Console.WriteLine("nPlease enter contestant {0}'s skill, S for singing, D for dancing, M for musical instrument and O for other", (x + 1));
                    string type = Console.ReadLine().ToUpper();
                    if (type == "S" || type == "D" || type == "M" || type == "O")
                    {
                        contestantCode[x] = type;
                        correct = true;
                    }
                    else
                    {
                        Console.WriteLine("{0} is not valid code", type);
                    }
                    int dance = 0;
                    int instrument = 0;
                    int sing = 0;
                    int other = 0;
                    int a = 0;
                    string entry;
                    for (a = 0; a < contestantCode.Length; ++a)
                    {

                        if (contestantCode[a] == "O")
                        {
                            ++other;
                        }
                        else if (contestantCode[a] == "S")
                        {
                            ++sing;
                        }
                        else if (contestantCode[a] == "D")
                        {
                            ++dance;
                        }
                        else if (contestantCode[a] == "M")
                        {
                            ++instrument;
                        }

                        Console.WriteLine("The types of talents are:");
                        Console.WriteLine("Singing {0}", sing);
                        Console.WriteLine("Dancing {0}", dance);
                        Console.WriteLine("Musical instrument {0}", instrument);
                        Console.WriteLine("Other {0}", other);
                        Console.WriteLine("nPlease enter a skill code 'S' 'D' 'M' 'O' to see a list of contestants with that skill or enter 'Z' to exit");
                        entry = Console.ReadLine().ToUpper();

                        while (entry != "Z")
                        {
                            if (entry != "S" && entry != "D" && entry != "M" && entry != "O")
                            {
                                Console.WriteLine("{0} is not a valid code.", entry);
                                Console.WriteLine("nPlease try again: Enter a VALID skill code 'S' 'D' 'M' 'O' to see a list of contestants with that skill or 'Z' to exit");
                                entry = Console.ReadLine().ToUpper();
                                if (entry == "Z")
                                    break;
                            }

                            for (int b = 0; b < contestantCode.Length; ++b)
                            {
                                if (entry == contestantCode[a])
                                {
                                    if (entry == "S")
                                    {
                                        Console.WriteLine("Contestants with talent Singing are: ");
                                        Console.WriteLine(contestantNames[x]);
                                    }
                                    else if (entry == "M")
                                    {
                                        Console.WriteLine("Contestants with talent Musical instrument are: ");
                                        Console.WriteLine(contestantNames[x]);
                                    }
                                    else if (entry == "D")
                                    {
                                        Console.WriteLine("Contestants with talent Dancing are: ");
                                        Console.WriteLine(contestantNames[x]);
                                    }
                                    else
                                    {
                                        Console.WriteLine("Contestants with talent Other are: ");
                                        Console.WriteLine(contestantNames[x]);
                                    }
                                }

                            }
                            Console.WriteLine("nPlease enter a skill code 'S' 'D' 'M' 'O' to see a list of contestants with that skill or enter 'Z' to exit");
                            entry = Console.ReadLine().ToUpper();
                        }
                    }
                }

            }
        }
    }
}


Solution 1:[1]

My first tip is try to create all variables in the top of the function unless this isn't possible (for oversight). Second you should use the ForEach loops when looping through a list. This will allow you to access all the objects inside the loop. this will not work for arrays. If you can you should create a custom class named Contestant. and make it a list by contestantArray.ToList(), this will require you to add LINQ add this above the namespace

using System.Linq;

namespace MyNameSpace
{
    ...
}

See my example:

foreach (var contestant in contestants)
{
    var o = contestant.O; //example of accessing the properties in `contestant`
}

this will make your codes readability better. I'm having a hard time determining what your exact question is. But I think it would help you out if you simplified your example a bit.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 rbdeenk