0

I'm new to C# I'm using microsoft Visual Studio Express 2013 Windows Desktop edition and I was trying to make a quiz in which I ask the question and the user has to answer it so, here's the code and the error i get is "Cannot implicitly convert type 'string' to 'bool'" and this happens on the 2 if statements, I understand that a bool has either the value true or false however it's a string so why is it giving me this error? Any help should be appreciated. PS: I only included the part of the code in which i'm having the problem and this is the only code in the main class

Heres the code:

 Start:
        Console.WriteLine();
        Console.WriteLine();
        Console.WriteLine("Question 1: Test? type yes or no: ");
        String answer1 = Console.ReadLine();

        if (answer1 = "yes") {
            Console.WriteLine();
            Console.WriteLine("Question 2: Test? type Yes or no");
        }
        else if (answer1 = "no")
        {
            Console.WriteLine();
            Console.WriteLine("Wrong, restarting program");
            goto Start;
        }
        else {
            Console.WriteLine();
            Console.WriteLine("Error");
            goto Start;
        }
5
  • 3
    you must type == not = Commented Jul 14, 2015 at 13:01
  • 5
    = assigns, == compares Commented Jul 14, 2015 at 13:02
  • 2
    possible duplicate of Help converting type - cannot implicitly convert type 'string' to 'bool' Commented Jul 14, 2015 at 13:09
  • 2
    i think its not good to answer a duplicate question. or at least one answer is enough Commented Jul 14, 2015 at 13:11
  • Please don't use goto statements as Terrance mentions below. They are harmful to the environment and to small fury animals Commented Jul 14, 2015 at 13:47

4 Answers 4

6

in all of your if statements

if (answer1 = "yes")

should be

if (answer1 == "yes")

in c#, = is to assign a value, == is for comparison. Change it in all of your if statements and youll be fine

Sign up to request clarification or add additional context in comments.

Comments

1

Please have a look at this line:

if (answer1 = "yes") {

This will assign "yes" to answer1 first and then it's like

if(answer1) { // answer1 = "yes"

So now this will try to convert answer1 which is a string into a boolean, which the if statement requires. This does not work and throws the exception.

You'll have to do a comparison like this:

if(answer1 == "yes") {

or you could use equals like this:

if("yes".Equals(answer1)) {

and then do the same for the else if.

Comments

0

The immediate reason is that = assigns, not compares values as == does. So you can do

   if (answer1 == "yes") {
     ...
   }

However I'd prefer

  if (String.Equals(answer1, "yes", StringComparison.OrdinalIgnoreCase)) {
    ...
  }

in case user chooses "Yes" or "YES" etc. as the answer

Comments

0

this = is the assignment operator in C#
this == is a comparison operator in C#

for a full list of the operators in C# check this out. As an asside I would generally recommend againt using goto statements

All that being said your code should look something like this.

Start:
        Console.WriteLine();
        Console.WriteLine();
        Console.WriteLine("Question 1: Test? type yes or no: ");
        String answer1 = Console.ReadLine();

        if (answer1 == "yes")
        {
            Console.WriteLine();
            Console.WriteLine("Question 2: Test? type Yes or no");
        }
        else if (answer1 == "no")
        {
            Console.WriteLine();
            Console.WriteLine("Wrong, restarting program");
            goto Start;
        }
        else
        {
            Console.WriteLine();
            Console.WriteLine("Error");
            goto Start;
        }

Comments

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.