0

screen shot of my codes input

I am having a problem about structures. I am trying to calculate quadrengles parameter, area and whether it is square or not but code doesn't go to if else part of islem();. This is my homework about struct.

using System;
public struct Str
{
    private double val;
    public double Value
    {
        get { return val; }
        set { val = value; }
    }
    public double Oku()
    {
        return double.Parse(Console.ReadLine());
    }
}
public struct Dörtgen
{
    Str ak;
    Str bk;
    public Str Ake
    {
        get { return ak; }
        set { ak = value; }
    }
    public Str Bke
    {
        get { return bk; }
        set { bk = value; }
    }
    public void Dörtgen1()
    {
        Str rct = new Str();
        Console.WriteLine("\nenter sides a and be of the square: ");
        Console.Write("A side: ");
        ak.Value = rct.Oku();
        Console.Write("B side: ");
        bk.Value = rct.Oku();
    }
    public void İslem()
    {
        Console.WriteLine("parameter: {0}", (Ake.Value + Bke.Value) * 2);
        Console.WriteLine("area:  {0}\n", Ake.Value * Bke.Value);
        if (Ake.Value == Bke.Value)
        {
            Console.WriteLine("this is a square");
        }
        else if (Ake.Value == Bke.Value)
        {
            Console.WriteLine("this is not a square");
        }
    }
}
public class Program
{
    static void Main()
    {
        var drg = new Dörtgen();
        drg.Dörtgen1();
        Console.WriteLine();
        Console.WriteLine("parameter and area");
        drg.İslem();
    }
}
Dharman
  • 30,962
  • 25
  • 85
  • 135
  • Your `elsif` is the same as your `if` (you wrote `Ake.Value == Bke.Value` in both of them), this is why the else part is never entered. Here you only need to write `else { ... }`, you don't need an `else if(...){...}` – Rafalon Apr 08 '21 at 08:39
  • ı didnt notice that,thanks. – Ahmet Alper Kızıltunç Apr 08 '21 at 08:40
  • 1
    Note: `struct` types usually represent "values" - your types should probably be `class`, not `struct` – Marc Gravell Apr 08 '21 at 09:06
  • If you are learning I would recommend reading why [mutable structs are evil](https://stackoverflow.com/questions/441309/why-are-mutable-structs-evil) – JonasH Apr 08 '21 at 09:08
  • Ok, My comment is not related to your question at all but: Sometimes you are using _U+0131 : LATIN SMALL LETTER DOTLESS I_. It may look like nothing but this. and the capitlize version, The infamous _U+0130_ is the thing that will make you hit your head at unit test. https://stackoverflow.com/questions/796986/what-is-the-turkey-test – Self Apr 08 '21 at 09:23

1 Answers1

0
if (Ake.Value == Bke.Value)
{
    Console.WriteLine("this is a square");
}
else if (Ake.Value == Bke.Value)
{
    Console.WriteLine("this is not a square");
}

Should be:

// Sides are equal: it's a square
if (Ake.Value == Bke.Value)
{
    Console.WriteLine("this is a square");
}
// didn't enter the "square" branch, so it's not a square
else
{
    Console.WriteLine("this is not a square");
}

Explanation: Besides the fact that you wrote Ake.Value == Bke.Value in both if and else if, you don't actually need to check anything if the first if is false, you can just go to the else branch.

Rafalon
  • 4,450
  • 2
  • 16
  • 30