Search code examples
c#stringif-statementdice

Dice roll console app returns errors in C#


I'm trying to write a console app, that does dicerolls with multiple types of dice, and after having done that, asks the user to confirm a new roll, or quit.

Here is minimal part that shows the problem:

string rolltype = "bob";
if (rolltype = "d4") // <-- error on this line
{
    Console.WriteLine("d4 roll is {0}", d4);
}

Code produces following error at compile time:

cannot implicitly convert type string to bool

Complete source:

namespace diceroll
{
    class Program
    {
        static void Main(string[] args)
        {
            Random rand = new Random();
            int d4 = rand.Next(1, 5);
            int d6 = rand.Next(1, 7);
            int d8 = rand.Next(1, 9);
            int d10 = rand.Next(1, 11);
            int d12 = rand.Next(1, 13);
            int d20 = rand.Next(1, 21);
            string rolltype;
            Console.WriteLine("what do you want to roll?");
            rolltype = (Console.ReadLine());
            Console.WriteLine("your choice is {0}", rolltype);
            if (rolltype = "d4")
            {
                Console.WriteLine("d4 roll is {0}", d4);
            }
            else { }
            Console.ReadKey();
        }
    }
}

What I wish to achieve here, is the console asking for the type of roll, and upon being given that, it returns you a random number. The (rolltype = "d4" ) returns an error "cannot implicitly convert type string to bool".


Solution

  • "cannot implicitly convert type string to bool", and i have no idea how to fix it

    this is because the code below doesn't result to a boolean value, below you're trying to assign rolltype to the string "d4" hence the error.

    if (rolltype = "d4")
    

    what you want is this:

    if (rolltype == "d4")
    

    Also is there a more elegant way of going at this than writing 6 separate if statements for the rolltypes?

    Sure, we can use an array to store the possible options and then just loop over it.

    Step 1 - create the array:

    string[] myArray = {"d4","d6","d8","d10","d12","d20"};
    

    now your code becomes like this:

    Random rand = new Random();
    int d4 = rand.Next(1, 5);
    int d6 = rand.Next(1, 7);
    int d8 = rand.Next(1, 9);
    int d10 = rand.Next(1, 11);
    int d12 = rand.Next(1, 13);
    int d20 = rand.Next(1, 21);
    string[] myArray = {"d4","d6","d8","d10","d12","d20"};
    

    Step 2 - loop over it to find if the entered value is equal to any of those within the array.

    foreach(string str in myArray){
       if (rolltype == str){
          // do something
          break; // if we get here then we don't need to loop any further
       }
    }
    

    now your code becomes like this:

    Random rand = new Random();
    int d4 = rand.Next(1, 5);
    int d6 = rand.Next(1, 7);
    int d8 = rand.Next(1, 9);
    int d10 = rand.Next(1, 11);
    int d12 = rand.Next(1, 13);
    int d20 = rand.Next(1, 21);
    string[] myArray = {"d4","d6","d8","d10","d12","d20"};
    string rolltype;
    Console.WriteLine("what do you want to roll?");
    rolltype = (Console.ReadLine());
    Console.WriteLine("your choice is {0}", rolltype);
    
    foreach(string str in myArray){
        if (rolltype == str){
              // do something
            break; // if we get here then we don't need to loop any further
        }
    }
    

    Another good solution suggested by Chris within the comments is to simply take the number out of the string. This will obviously reduce the amount of rand.Next() you currently have.

    Example:

    int dieType = int.Parse(rollType.Substring(1)); 
    int result = rand.Next(1,dieType+1);