Click here to Skip to main content
15,908,907 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
C#
<pre>
       static void Main(string[] args)
        {
            Random newRandom = new Random();
            int slumpTal = newRandom.Next(1, 101);
            bool[] boolVektor = new bool[slumpTal];
            
            for (int i = 0; i < boolVektor.Length; i++)
            {
                int slump = newRandom.Next(0,2);
                if (slump == 0)
                    boolVektor = true;
                else
                    boolVektor = false; 
            }
            
            Console.Write("Skriv in sökOrd");
            string searchWord = Console.ReadLine();
            bool search = false;
            for (int i = 0; i < boolVektor.Length; i++)
            {
                if (boolVektor[i] == search)
                {
                    Console.WriteLine("Följande hittades" + boolVektor[i]);
                    search = true;
                }
                if (!search)
                {
                    Console.WriteLine("Din sökning misslyckades");
                }
            }


What I have tried:

I want that the user could search true or false, so that the user could know how many true or false is saved in the boolVektor. I have to use convert.Toboolean. But I don't know how. At the moment I'm getting this thing "Cannot implicity convert type bool to bool[]"
Posted
Updated 21-Nov-17 20:33pm

Your declared boolVektor as an array of booleans, not a single boolean.

In your loop, you've got:
C#
for (int i = 0; i < boolVektor.Length; i++)
{
    int slump = newRandom.Next(0,2);
    if (slump == 0)
        boolVektor = true;
    else
        boolVektor = false; 
}

Ummmm... you can't assign a value to the entire array. You have to use an index on the array:
C#
for (int i = 0; i < boolVektor.Length; i++)
{
    int slump = newRandom.Next(0,2);
    if (slump == 0)
        boolVektor[i] = true;
}

You also don't need to set the value to false as it is the default value for a boolean.
 
Share this answer
 
Comments
Member 13510990 21-Nov-17 19:15pm    
As though I'm a new beginner I would like if you explain a bit more about what you mean by "You also don't need to set the value to false as it is the default value for a boolean."
Dave Kreskowiak 21-Nov-17 19:39pm    
When you declare a variable, it has a default value. For integers, it's 0. For booleans, it's false, which is represented by a 0.

If the value is already 0, setting it to 0 against doesn't really get you anything, does it?
Hi,
I don't get exactly for what purpose you want your user to search for true and false.
But changing the following lines of code where you're getting error will resolve your error.

for (int i = 0; i < boolVektor.Length; i++)
               {
                   int slump = newRandom.Next(0, 2);
                   if (slump == 0)
                       boolVektor[i] = true;
                   else
                       boolVektor[i] = false;
               }


As you're beginner I give a try to make it clear for you.

Do you know what is an array?, I guess you know that's why you have used a for loop, because we use loops normally when we work on the collections. Array is a collection of something. Take it like a plural not a singular. You were assigning a single value to the whole collection, in your case this collection can be of any random length between 1 to 101, since you have defined it.
int slumpTal = newRandom.Next(1, 101);

Now when you were trying to assign a single value to the whole array at once it was giving you an error. Try assigning values one by one, index by index as I have shown you.
Hope this will help, if any confusion is left I am open for more discussions.
Happy learning!
 
Share this answer
 
Comments
Member 13510990 22-Nov-17 2:34am    
Thnx. I understood. But the problem I have right now is whenever I search a number the first value I'm getting is false. I don't know why. This is what my code look like at the moment.
Afzaal Ahmad Zeeshan 22-Nov-17 13:27pm    
The first value in the list, or the first value anytime?

Maybe that is because of the random generation.
C#
<pre> Random newRandom = new Random();
            int slumpTal = newRandom.Next(1, 101);
            bool[] boolVektor = new bool[slumpTal];

            //var search = Convert.ToBoolean(Convert.ToInt32("0"));

            for (int i = 0; i < boolVektor.Length; i++)
            {
                int slump = newRandom.Next(0, 2);
                if (slump == 0)
                    boolVektor[i] = true;
                else
                    boolVektor[i] = false;
            }
            {

                Console.Write("Skriv in sökOrd: ");
                string searchWord = Console.ReadLine();
                bool search = false;
             
                for (int i = 0; i < boolVektor.Length; i++)
                {
                    if (boolVektor[i] == search)
                    {
                        Console.WriteLine("Följande hittades: " + boolVektor[i]);
                        search = true;
                    }
                    if (!search)
                    {
                        Console.WriteLine("Din sökning misslyckades");
                    }
                }
                Console.ReadLine();
            }
        }
    }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900