-2

For a school assignment I have to write a program where the program calculates the area of a right triangle. You get the three sides. So I would like to determine with pythagoras what the base is and what the height is. Every time I enter this via assignment I get CS0029 CS0029 cannot implicitly convert type 'double' to 'bool'. Has someone an idea?

class Program
{
    static void Main(string[] args)
    {
      
       
        void Opp(double D, double E,out double O)
        {               
         O = (D * E / 2);               
        }
        void Pyth (double F, double G, out double H)
        {
            H = Math.Sqrt(Math.Pow(F, 2) + Math.Pow(G, 2));
        }
        Console.WriteLine("Geef de waarde van driehoek 1 in: ");
        Console.WriteLine("Zijde a");
        double A1 = double.Parse(Console.ReadLine());

        Console.WriteLine("Zijde b");
        double B1 = double.Parse(Console.ReadLine());

        Console.WriteLine("Zijde c");
        double C1 = double.Parse(Console.ReadLine());

        double DH1;
        double PA1 ;
        double PB1;
        double PC1;
        double DH2;
        double P2;

        Pyth(A1, B1, out PC1);
        Pyth(A1, C1, out PB1);
        Pyth(C1, B1, out PA1);

        if (C1 = PC1)
        {
            Opp(A1, B1, out DH1);
            Console.WriteLine("Oppervlakte driehoek 1 is: " + DH1 + " cm²");
        }
        else if (B1 = PB1)
        {
            Opp(A1, C1, out DH1);
            Console.WriteLine("Oppervlakte driehoek 1 is: " + DH1 + " cm²");
        }
        else if (A1 = PA1) {
            Opp(C1, B1, out DH1);
            Console.WriteLine("Oppervlakte driehoek 1 is: " + DH1 + " cm²");
        }
        else { Console.WriteLine(" error"); }


        Opp(A1,B1,out DH1);
        Console.WriteLine("de oppervlakte van driehoek 1 is " + DH1 + "cm²");

        double pyth = Math.Sqrt(Math.Pow(A1, 2) + Math.Pow(B1, 2));
        Console.WriteLine("controle");
        Console.ReadLine();
    }
}

}

  • 2
    `if (C1 = PC1)` ...this and all the other expressions in your `if` statements are incorrect. `=` is the assignment operator - i.e. you're telling it to change the value of C1 to be the value of PC1. What you probably want is `==` - this is a **comparison** operator, it tells the program to compare the two values and see if they are equal or not. This is also the root of your error, because the output of `C1 = P1` would be a double, and you can't use a double in an `if` statement, because it doesn't have a true/false outcome like a bool does. – ADyson Dec 03 '20 at 11:39

1 Answers1

0

You have to correct your if statements. In c# to compare 2 elements you need the comparison operator (==) So correct them like this

if (C1 == PC1)
{
   //DoStuffHere
}
Sethlans
  • 407
  • 1
  • 5
  • 20