3

i am trying to learn, (im a newb to coding)

i wrote this

static void Main(string[] args)
{
    string username = "James";
    string[] Userclass = new string[3] { "Mage", "Warrior", "Assasin" };

    Console.WriteLine("What class will you be? You can choose from Mage, Warrior, Or Assasin: ");


    if (Userclass.Contains("Mage"))
    {
        String Message = "You are a strong Willed Mage " + username;
        Console.WriteLine(Message);
    }
     if (Userclass.Contains("Warrior"))
    {
        String Message = "Your are a valiant Warrior " + username;
        Console.WriteLine(Message);
    }
     if (Userclass.Contains("Assasin"))
    {
        String Message = "You are a vigilant Assasin " + username;
        Console.WriteLine(Message);

    }
    Console.ReadLine();
}

however if i define:

string Userclass = Console.Readline(); 

I get the error that it

can't convert string[] to string

thanks for any help!

Mong Zhu
  • 23,309
  • 10
  • 44
  • 76
  • 5
    `Console.ReadLine` returns a single string. You've defined your variable `Userclass` to be an array (`[]`) of strings. The types of the things on the right side of the assignment operator (`=`) need to match (or be compatible with) the types of the variables on the left side of the assignment operator. – Kenneth K. Dec 04 '18 at 15:34
  • 1
    You should keep a clear distinction between two things: the class that the user chooses, and the options that the user has. The first is a single String. The second is your array of strings. – S.L. Barth is on codidact.com Dec 04 '18 at 15:36
  • does that mean that i cannot use an array of strings to make a choice? – James Sketch And The Beast Sul Dec 04 '18 at 15:55
  • unrelated, but "assasin" is written as "assassin" :) – Dennis Vanhout Dec 04 '18 at 16:14
  • 1
    considering you're learning to code, I'm gonna give you these pointers as well. use "string" instead of "String", the capital usage refers to methods and Classes, whereas you intend to refer to a type. Also, when naming objects, use lowercase (e.g. "userclass" instead of "Userclass"), there's no real reason to do so, except for that it follows the general naming conventions. :) – Dennis Vanhout Dec 04 '18 at 16:21
  • 1
    @DennisVanhout [`string` is an alias for `String`](https://stackoverflow.com/questions/7074/what-is-the-difference-between-string-and-string-in-c/32893650)...they are identical in function. Some actually suggest using `String` for cross-language compatibility. – Kenneth K. Dec 04 '18 at 18:03
  • 1
    @KennethK. according to that logic, you also should/could be using Boolean and Int32 instead of bool and int. – Dennis Vanhout Dec 05 '18 at 08:35
  • @DennisVanhout Yep. – Kenneth K. Dec 05 '18 at 15:34

2 Answers2

3

Okay, a few things:

Your UserClass is an array. It will hold items, but unless you are appending to it, you cannot write user input to it. Here is a reference for Arrays in C# from MSDN.

You are using .Contains() on a string array instead of a string. While logically, yes, the string array does contain those values. Although, if it actually ran/compiled, all the if statements would run true rather than choosing from user input.

Which leads me to my next thing- you ask for user input but never actually allow for it within the as-shown Main() method. The most common way (that I've seen) is something like:

string input = Console.ReadLine();

Which, I see you are trying to implement, so that isn't an issue :)


This code should work for (and I'll break down my changes):

static void Main(string[] args)
{
    string userName = "James";
    string[] userClass = new string[3] { "mage", "warrior", "assassin" };

    Console.WriteLine("What class will you be? You can choose from Mage, Warrior or Assassin:");
    string input = Console.ReadLine();

    if (input.ToLower() == userClass[0])
    {
        string Message = "You are a strong Willed Mage " + userName;
        Console.WriteLine(Message);
    }
    else if (input.ToLower() == userClass[1])
    {
        string Message = "Your are a valiant Warrior " + userName;
        Console.WriteLine(Message);
    }
    else if (input.ToLower() == userClass[2])
    {
        string Message = "You are a vigilant Assassin " + userName;
        Console.WriteLine(Message);
    }
    else
        Console.WriteLine("No proper class selected...");

    Console.ReadLine();
}

string userName remains the same along with your string[] userClass (I have altered the capitalization to camel-casing). There is no issue (that I see) in having these userClasss stored within an array , as long as you are checking against the array properly.

Instead of checking if the string[] userClass array contains these items, because we know it does as we've written it and as stated before, it would always run true. Instead, we check to see if the user input matches something within the array.

I have created string input = Console.ReadLine() to allow for user input, and then I check input against the string[] userClass values. I have added .ToLower() and changed the array values to all lower case. This way, if a user inputs mAgE instead of Mage , there will not be an error thrown.


Another way to go about it, if you are wanting to avoid using an array , is to use a switch:

switch (input.ToLower())
{
    case "mage":
        Console.WriteLine($"You are a strong Willed Mage {userName}");
        //assign user class
        break;
    case "warrior":
        Console.WriteLine($"Your are a valiant Warrior {userName}");
        //assign user class
        break;
    case "assassin":
        Console.WriteLine($"You are a vigilant Assasin {userName}");
        //assign user class
        break;
}
Jaskier
  • 1,075
  • 1
  • 10
  • 33
2

Here you go, I wrote comments explaining each part of the program, let me know if you can't understand something.

static void Main(string[] args)
{
    Console.Write("Enter Username: "); // we ask for the username here, you can do it with Console.WriteLine() as well
    string username  = Console.ReadLine();  // get the username
    Console.Write("What class will you be? You can choose from Mage, Warrior, Or Assasin: "); // ask for the class, also same as above, you can use Console.WriteLine()
    string userclass = Console.ReadLine();   // get the class

    // Possible character classes (we use this as a reference to check the class the user gives us later)
    // also set the string representing each class as lower case characters to avoid looping or unnecessary string concatenation later
    string[] charClasses = new string[3] { "mage", "warrior", "assasin" }; // make sure the class given is within our possible character classes

    // here we check that the class given is within the bound of our possible classes and convert
    // the userclass to its lower case equivalent to match it with those in the array
    if (Array.IndexOf(charClasses, userclass.ToLower()) != -1)   
    {
         // if the class is acceptable then attach it to the message along with the username via string concatenation
        String Message = "You are a strong Willed "+userclass + " " + username; 
        Console.WriteLine(Message);  // print the message
    }
}
Bargros
  • 521
  • 6
  • 18
  • oh thats a much cleaner way to do it , bit more advanced than i can do atm.. but thank you, ill play with that context too. – James Sketch And The Beast Sul Dec 04 '18 at 16:29
  • 1
    I added this example so you could see the logic behind the code I wrote and compare it to yours, so you could keep modifying it (And added comments so you understand what's happening). I basically used the same concepts and data structures you used but changed the logic so you could see that heavy reliance on data structures and language constructs (such as multiple **if** statements) is not the way. But as you are a beginner everything is allowed I guess, still glad i could help or I hope I did :) – Bargros Dec 04 '18 at 16:39
  • absolutely! yea i agree with your method, ideally i would prefer to keep things as simple as possible as i plan on making this a really lengthy text adventure, someday with actual images and scenarios so it can become really long! im still trying to understand arrays and stuff so im trying to implement different things in different ways in the part (Array.IndexOf(charClasses, userclass.ToLower()) != -1) what is that doing and what is the !=-1 saying? – James Sketch And The Beast Sul Dec 04 '18 at 17:31
  • 1
    `Array.IndexOf` is a method of the class Array and all objects declared like this `int[] or string[]` implement that class, the method **IndexOf** take an array as the first parameter and value as the second, the method looks for that value within the array by looking for the first value that matches. If there are matches the method returns a number greater or equal to 0, otherwise it returns -1, what I did there is basically check that is not -1 to execute whats inside the **if** statement, otherwise it prints nothing. Try adding a class that doesn't exist in the array, it prints nothing. – Bargros Dec 04 '18 at 17:42
  • Not to be a beggar but can i get an up vote if the answer helped you? – Bargros Dec 05 '18 at 13:16