C# Console application help

Updated on June 16, 2018 in [A] C# .Net
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
3 on June 13, 2018

I’m relatively new to c# and I am struggling with something. I’m trying to test myself and im making a little console application game. The details of it aren’t really important but there’s something that just doesn’t work.

 

there’s a part of it where I use a type writer effect, and that bit works fine. The problem happens when I try to expand on it. Basically what im trying to do is make it so the program waits a bit longer when it hits certain punctuation marks such as full stops and commas but no matter what I do it just doesn’t seem to work. This is what the code looks like right now and this works perfectly fine and how it is meant to:

string entry1 = "This is a test";
 
Console.Write("Entry:  ");
 
for (int I = 0; I < entry1.Length; i++) 
 
{
 
       Console.Write(entry1[ i ]);
 
       System.Threading.Thread.Sleep(100);
 
}

this is what I was tried doing to create the effect I was looking for:

string entry1 = "This is a test";
 
Console.Write("Entry:  ");
 
for (int I = 0; i < entry1.Length; i++) 
 
{
 
     if (entry1[ i ] == " . ")
 
     {
 
            Console.Write(entry1[ i ]);
 
            System.Threading.Thread.Sleep(300);
 
     }
 
     else if (entry[I] = ",")
 
     {
 
            Console.Write(entry1[I]);
 
            System.Threading.thread.Sleep(200);
 
     }
 
     else
 
     {
 
           Console.Write(entry1[I]);
 
           System.threading.Thread.Sleep(100);
 
     }
 
}

The error that keeps coming up is that on the if statement, it says, ‘operator “==” cannot be applied to operands of type “char” and “string”‘. I think this is because I use like this, ‘entry[i], because when I get rid of the array thingy [i] it works. The reason im annoyed is because it works when its being used in the loop to display the text but not when checking if that part of the entry is equal to something.

 

I know this was a long read and im sorry if it was. I just really want this to work. if anybody could help in any way it would be appreciated.

  • Liked by
Reply
2 on June 14, 2018

So, a string is an array of characters with special methods attached to it. When you are iterating through the string “hello”, you are iterating over characters. You cannot compare a character and a string, as they are two different objects. At least, not with “==”.

Use something like:

if (entry1[i].Equals('.')) {}

and see if that performs better.

Also, for challenge mode:

Create a list of characters that are “pause” characters. Checks for whether the list contains current character, rather than writing an ‘if’ for every special character you want an effect for. If you want the pause to differ per character, use a Dictionary instead:

IDictionary pauseChars = new Dictionary<char, int>();

Hope this helps! Have fun.

on June 14, 2018

It worked! Thank you very much. I’ll try playing around with the challenge you gave me as well. It’ll give me something to work towards. Again, thanks for your help!

on June 16, 2018

Hassan, what IDE are you using??

Show more replies
  • Liked by
Reply
Cancel