Click here to Skip to main content
15,887,444 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Basically my code is going through both the if and else statement the first time it loops.

Basically it is going through if(length = length) and if(length < length)

[^]

The first time it runs fine but the second time it doesnot

What I have tried:

if (GuessChar.Length == ActualChar.Length)//GIVING ERROR
            {
                //Check Each Element
                for (int i = 0; i < WordLenght; i++)
                {
                    for (int p = 0; p < WordLenght; p++)
                    {
                        if (GuessChar[i] == ActualChar[p])
                        {
                            LetterGuessed++;
                        }
                    }
                }
                //Repeating in a single array
                var duplicateDict = new Dictionary<char, int>();
                foreach (var ch in GuessChar)
                {
                    if (duplicateDict.ContainsKey(ch))
                    {
                        duplicateDict[ch]++;
                    }
                    else
                    {
                        duplicateDict.Add(ch, 1);
                    }
                }

                foreach (var ch in duplicateDict.Where(x => x.Value > 1))
                {
                    //Console.WriteLine(string.Format("Duplicate Character {0}, Count {1}", ch.Key, ch.Value));
                    repeated++;
                }
                if (repeated == 1)
                {
                    repeated = 0;
                }
                for (int i = 0; i < WordLenght; i++)
                {
                    if (ActualChar[i] == GuessChar[i]) Victory = true;
                }

            }
            else if (GuessChar.Length > WordLenght)
            {
                //THIS ONE IS GIVING THE ERROR
                Console.WriteLine("Wrong Length, Actual Lenght is : Shorter");
            }
            else if (Guess.Length > WordLenght)
            {
                Console.WriteLine("Wrong Length, Actual Lenght is : Longer");
            }
Posted
Updated 15-May-20 0:19am
v3
Comments
Richard MacCutchan 13-May-20 7:50am    
There are a few ifs and elses in your code. Which ones are you referring to and what conditions are being checked?

C#
else if (GuessChar.Length > WordLenght)
{

    Console.WriteLine("Wrong Length, Actual Lenght is : Shorter");
}
else if (Guess.Length > WordLenght) // *** this one should use < operator
{
    Console.WriteLine("Wrong Length, Actual Lenght is : Longer");
}

You are using the same compare operator for two different tests.
 
Share this answer
 
Comments
Maciej Los 13-May-20 8:32am    
Hawk eye!
Richard MacCutchan 13-May-20 9:42am    
If I changed my name I would soon be found out. :(
NotAComputerScienceStudent 13-May-20 8:33am    
if (GuessChar.Length == ActualChar.Length) and else if (GuessChar.Length > WordLenght) both of these statements are being excecuted.
Richard MacCutchan 13-May-20 9:41am    
Yes, because they are testing different situations: first ActualChar.Length, second WordLenght. You really need to look more closely at the code you have written.
F-ES Sitecore 13-May-20 9:09am    
One is testing GuessChar and the other just Guess.

It seems to me that you are doing things the hard way.


C#
//Check Each Element
              for (int i = 0; i < WordLength; i++)
              {
                for (int p = 0; p < WordLength; p++)
                 {
                   if (GuessChar[i] == ActualChar[p])
                    {
                      LetterGuessed++;
                    }
                 }
              }

Can be replaced by


C#
foreach (char c in GuessChar)
{
    LetterGuessed = ActualChar.Contains(c) ? LetterGuessed+=1: LetterGuessed;
}

And this


C#
//Repeating in a single array
var duplicateDict = new Dictionary<char, int>();
foreach (var ch in GuessChar)
{
    if (duplicateDict.ContainsKey(ch))
    {
        duplicateDict[ch]++;
    }
    else
    {
        duplicateDict.Add(ch, 1);
    }
}

foreach (var ch in duplicateDict.Where(x => x.Value > 1))
{
    //Console.WriteLine(string.Format("Duplicate Character {0}, Count {1}", ch.Key, ch.Value));
    repeated++;
}

Can be refracted to


C#
var letters = GuessChar.GroupBy(c => c, (k, x) => (Letter:k, DuplicateCount:x.Count()-1));
 foreach(var letter in letters)
 {
     repeated += letter.DuplicateCount;
 }

Not sure why you are setting repeated to 0 if it is 1 but you can remove the if statement by doing this


C#
//if (repeated == 1)
//{
//    repeated = 0;
//}
repeated= repeated==1?0:repeated;

This loop should have an early exit option


C#
for (int i = 0; i < WordLength; i++)
              {
                  if (ActualChar[i] == GuessChar[i]) Victory = true;
              }
for (int i = 0; i < WordLength; i++)
              {
                  if (ActualChar[i] == GuessChar[i])
                   {
                    Victory = true;
                    break;
              }

Finally, the last else if statement


C#
else if (GuessChar.Length > WordLength)
{
    //THIS ONE IS GIVING THE ERROR
    Console.WriteLine("Wrong Length, Actual Lenght is : Shorter");
}
else if (GuessChar.Length > WordLength)
{
    Console.WriteLine("Wrong Length, Actual Lenght is : Longer");
}

Can be replaced by


C#
else
          {
              string substring = GuessChar.Length > WordLength ? "Shorter" : "Longer";
              Console.WriteLine($"Wrong Length, Actual Length is : {substring}");

          }
 
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