-1
using System;

namespace Codes
{
    class Program
    {
        static void Main(string[] args)
        {
            String name = RandomNames();
            Console.Write(name);
            Console.ReadKey();
        }

        public static string RandomNames()
        {
            Random numGen = new Random();
            string vowel = "aeiou";
            string conso = "bcdfghjklmnpqrstvwxyz";
            int nameLen = numGen.Next(4,10);

            string result = "";
            for (int i = 0; i < nameLen; i++) 
            {
                if (i % 2) 
                {
                    result += vowel[numGen.Next(0, 4)]; // 4 is final index of vowel.
                } 
                else 
                {
                    result += conso[numGen.Next(0, 20)]; // 20 is final index of consonants.
                }
            }
            return result;
        }
    }

it says the "i" in if statement cannot implictly convert type "int" to "bool" and I don't know whats the problem with that because it's already an int in for loop

Mr.Man
  • 1
  • 3
  • 1
    `if (i % 2 != 0)` – 001 Oct 29 '20 at 14:48
  • 2
    `i % 2` results in an int, to do the check, use `i % 2 != 0` – Marcus Oct 29 '20 at 14:49
  • Modolus returns int not bool – Bill Gauvey Oct 29 '20 at 14:51
  • 1
    C and C++ have no problem interpreting integers as bools (`0` is `false`, anything else is `true`) But that sometimes led to subtle and difficult bugs, so C# dropped that ability. Conditionals, like `if` statements, can no longer work on an integer like a bool. They must have ***explicit*** boolean values, like the result of `==`, `<`, `>`, or `!=`. – abelenky Oct 29 '20 at 14:54
  • Near duplicate: [cannot implicitly convert type 'int' to 'bool' and i dont understand](https://stackoverflow.com/q/26984833/3744182). The error there is with the assignment operator not the modulus operator. – dbc Oct 29 '20 at 15:24
  • Does this answer your question? [cannot implicitly convert type 'int' to 'bool' and i dont understand](https://stackoverflow.com/questions/26984833/cannot-implicitly-convert-type-int-to-bool-and-i-dont-understand) – Svirin Oct 29 '20 at 15:41

1 Answers1

2

i % 2 is of type int, not bool, C# cannot automatically make the conversion, so simply try:

i % 2 != 0
AndreiXwe
  • 723
  • 4
  • 19