0

Jungle Camping challenge C#

I don't learn C# but this annoys me. Whenever I run this code, it outputs an error telling me to use the "fixed" keyword when I don't even know what it is. I'm bad at arrays. string a, b, c, d; a = "Grr"; b = "Rawr"; c = "Ssss"; d = "Chirp"; string e[10]; e[0] = Console.ReadLine(); e[1] = Console.ReadLine(); e[2] = Console.ReadLine(); e[3] = Console.ReadLine(); e[4] = Console.ReadLine(); e[5] = Console.ReadLine(); e[6] = Console.ReadLine(); for (int i = 0; i < 7; i++) { if (e[i] == a) { Console.Write("Lion "); } if (e[i] == b) { Console.Write("Tiger "); } Is there anything wrong in this code?

6th May 2020, 7:22 AM
Epsilon ︻╦̵̵͇̿̿̿̿╤──
Epsilon ︻╦̵̵͇̿̿̿̿╤── - avatar
5 Respostas
+ 1
Epsilon You're declaring string e[] 2 times. Remove first e[10] declaration.. In Second array declaration statement, array size declared automatically, depending on how many words in Splitted line of string.. And add space in output.. As you did previously..
6th May 2020, 5:31 PM
Jayakrishna 🇮🇳
+ 2
Changed the code into this: string a, b, c, d; a = "Grr"; b = "Rawr"; c = "Ssss"; d = "Chirp"; string e[] = new string[10]; string[] e = Console.ReadLine().Split(" "); for (int i = 0; i < e.Length; i++) { if (e[i].Equals(a)) { Console.Write("Lion"); } if (e[i].Equals(b)) { Console.Write("Tiger"); } if (e[i].Equals(c)) { Console.Write("Snake"); } if (e[i].Equals(d)) { Console.Write("Bird"); } } It still gives an error saying "Bad array declarator". Did I miss something Jayakrishna🇮🇳 ?
6th May 2020, 12:14 PM
Epsilon ︻╦̵̵͇̿̿̿̿╤──
Epsilon ︻╦̵̵͇̿̿̿̿╤── - avatar
+ 1
Array declaration : string e[] =new string[10] ; This is static way of array declaring and allocating.. Problem have only one line of input with varying number of words... So you need to read and split the line into words, then display results accordingly.. Use always Equals method to compare strings.. == compare only refference not contents.. So the lines of edited proper code is.. For splitting : string[] e = Console.ReadLine().Split(); for (int i = 0; i < e.Length; i++) { //for array length.. if (e[i].Equals(a)) { //comparing.. Then complete it to given task... You need to check for the 4 types...
6th May 2020, 11:07 AM
Jayakrishna 🇮🇳
6th May 2020, 12:08 PM
Epsilon ︻╦̵̵͇̿̿̿̿╤──
Epsilon ︻╦̵̵͇̿̿̿̿╤── - avatar
0
Thanks Jayakrishna🇮🇳 , it worked!
7th May 2020, 11:01 AM
Epsilon ︻╦̵̵͇̿̿̿̿╤──
Epsilon ︻╦̵̵͇̿̿̿̿╤── - avatar