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!

7
  • 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. Commented Dec 4, 2018 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. Commented Dec 4, 2018 at 15:36
  • 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. :) Commented Dec 4, 2018 at 16:21
  • 1
    @DennisVanhout string is an alias for String...they are identical in function. Some actually suggest using String for cross-language compatibility. Commented Dec 4, 2018 at 18:03
  • 1
    @KennethK. according to that logic, you also should/could be using Boolean and Int32 instead of bool and int. Commented Dec 5, 2018 at 8:35

2 Answers 2

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;
}
Sign up to request clarification or add additional context in comments.

Comments

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
    }
}

3 Comments

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 :)
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?
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.