So, what I'm trying to do is to check if a letter already exists within an array, so I use a for loop and it works very well.

So if the character exists within the array, it will not add the letter you put in the input box into the array, but if it doesn't exist, then go ahead and add the letter to the array.

if (bokstav == enBokstav)
                    {
                        for (int arrIndex = 0; arrIndex < gjettet_bokstaver.Length; arrIndex++)
                        {
                            if (bokstav == gjettet_bokstaver[arrIndex])
                            {
                                MessageBox.Show("Du har allerede brukt den bokstaven");
                            }
                        }
                        if (bokstav != gjettet_bokstaver[array_size])
                        {
                            gjettet_bokstaver[array_size] += bokstav;
                            array_size++;
                            RiktigBokstav.Text += gjettet_bokstaver[(array_size - 1)];
                        }
                    }

So, my problem is that, if the letter exists in the array, there will come a poup box saying it's already used and it will still add a new character instead of stopping the script, sort of.

Thanks in advance :)

Recommended Answers

All 6 Replies

I was going to suggest using a flag in this if statement as to whether or not you'd found the character included, but I don't know if that's the best practice if (bokstav != gjettet_bokstaver[array_size]) . Just rereading it now, do you mean for array_size in the second if statement to be arrIndex? (realizing that you are usually dealing with the last character entered maybe it needs to be how you have it). Even though I know what the code is saying I'm unsure of the text and the variables, so apologies and hope that helped if even just a bit.

I think bokstav means letter and is of type char.
So gjettet_bokstaver is defined as an array of char? Or am I wrong? I would like to see the declaration it plus that of array_size
As jonsca already suggested gjettet_bokstaver[array_size] probably has to be gjettet_bokstaver[array_size-1])

Here is an example that I put together. I wrote a Remove Duplicate method that accepts a List as an overload. It will remove the remove the duplicates and send it back to its caller. Here goes.

static void Main(string[] args)
        {
            try
            {
                List<string> Letters = new List<string>();
                Letters.Add("a");
                Letters.Add("b");
                Letters.Add("b");

               

                foreach (string letter in RemoveDuplicates(Letters))
                {
                    Console.WriteLine(letter);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            Console.ReadKey();

        }
        static List<string> RemoveDuplicates(List<string> Letters)
        {
            Dictionary<string, int> uniqueStore = new Dictionary<string, int>();
            List<string> finalList = new List<string>();
            foreach (string currValue in Letters)
            {
                if (!uniqueStore.ContainsKey(currValue))
                {
                    uniqueStore.Add(currValue, 0);
                    finalList.Add(currValue);
                }
            }
            return finalList;
        }
/*
Also the output of the program is 

a
b

as duplicate b was removed from the List<T>.
*/

The issue here is that even though it does detect that the character exists in one of the array elements, it still continues the loop and adds the same character to the next available element.

Hope this clears some things up.

We answer your questions, could you be so kind to answer ours? Your last post does not clear things up. In your for loop the messagebox says "You have already used that letter" if you found a letter already in your array. After the loop has finished you enter a conditional statement I have questions about.(see post #3) Answer?

try:

if (bokstav == enBokstav)
                    {
                        bool letterExists = false;
                        for (int arrIndex = 0; arrIndex < gjettet_bokstaver.Length; arrIndex++)
                        {
                            if (bokstav == gjettet_bokstaver[arrIndex])
                            {
                                MessageBox.Show("Du har allerede brukt den bokstaven");
                                letterExists = true;
                            }
                        }
                        if (!letterExists)
                        {
                            gjettet_bokstaver[array_size] += bokstav;
                            array_size++;
                            RiktigBokstav.Text += gjettet_bokstaver[(array_size - 1)];
                        }
                    }
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.