0

I'm trying below code. It gives me a warning "Use of unassigned local variable 'hak' ". I guess I'm missing something here.

I want it to display "3 defa yanlış giriş yaptınız. Müşteri hizmetleri temsilcisiyle görüşünüz. İyi günler dileriz." when local variable "hak" equals to "0". But it always display "Hatalı Giriş Yaptınız. Lütfen tekrar deneyiniz. 2 hakkınız kalmıştır."

                   Console.Write("Seçmek istediğiniz 'Kullanıcı Adı'nı belirtiniz: ");
        string kullanici_adi = Console.ReadLine();
        FARKLISIFRE:
        Console.Write("Lütfen Şifrenizi giriniz: ");
        string sifre = Console.ReadLine();
        Console.Write("Girmiş olduğunuz şifreyi tekrar giriniz: ");
        string sifre2 = Console.ReadLine();

        int karsilastirma = String.Compare(sifre, sifre2);
        if (karsilastirma==0)
        {
            Console.WriteLine("Tebrikler! Kaydınız başarılı bir şekilde oluşturulmuştur.");
        }
        else
        {
            Console.WriteLine("Girmiş olduğunuz şifreler birbirinden farklıdır. Lütfen tekrar deneyiniz.");
            goto FARKLISIFRE;
        }

        Console.Write("Giriş yapmak için lütfen '1' seçeneğini giriniz: ");
        char komut = Convert.ToChar(Console.ReadLine());
        Console.Clear();
        if (komut=='1')
        {
            goto GIRIS;
        }

        else
        {
            Console.Write("Çıkış yaptınız. İyi günler dileriz.");
            goto END;
        }
        
        int hak = 3;

        GIRIS:
        Console.Write("Lütfen Kullanıcı Adınızı Giriniz: ");
        string kullanici_adi_giris = Console.ReadLine();
        Console.Write("Lütfen belirlemiş olduğunuz Şifrenizi giriniz: ");
        string sifre_giris = Console.ReadLine();

        int karsilastirma_k_adi = String.Compare(kullanici_adi, kullanici_adi_giris);
        int karsilastirma_sifre = String.Compare(sifre, sifre_giris);
        
        if (karsilastirma_k_adi == 0 && karsilastirma_sifre == 0)
        {
            Console.Write("Başarıyla Giriş Yaptınız. Hoşgeldiniz.");
        }
        else if (hak == 0)
        {
            Console.Write("3 defa yanlış giriş yaptınız. Müşteri hizmetleri temsilcisiyle görüşünüz. İyi günler dileriz.");
        }
        else
        {
            hak--;
            Console.WriteLine("Hatalı Giriş Yaptınız. Lütfen tekrar deneyiniz. {0} hakkınız kalmıştır.", hak);
            goto GIRIS;
        }
        
        END:
        Console.ReadKey();
    }
  • CS0165 is the Error Code. – Selçuk Özübek Jan 26 '21 at 17:37
  • I don't think the problem starts from here on. `hak` is initialized with an assignment starting here. I think you have another piece of code causing this warning. – clamchoda Jan 26 '21 at 17:44
  • I posted the full code. – Selçuk Özübek Jan 26 '21 at 17:47
  • I want it to display "3 defa yanlış giriş yaptınız. Müşteri hizmetleri temsilcisiyle görüşünüz. İyi günler dileriz." when local variable "hak" equals to "0". But it always display "Hatalı Giriş Yaptınız. Lütfen tekrar deneyiniz. 2 hakkınız kalmıştır." – Selçuk Özübek Jan 26 '21 at 17:47
  • "else if (hak == 0)" line causes the error code. – Selçuk Özübek Jan 26 '21 at 17:48
  • I will consider your advice on using refactoring. But i want to understand this error. – Selçuk Özübek Jan 26 '21 at 18:08
  • As you said, when i write "GIRIS: int hak = 3;", this time it always displays "Hatalı Giriş Yaptınız. Lütfen tekrar deneyiniz. 2 hakkınız kalmıştır." and there is no decrease. – Selçuk Özübek Jan 26 '21 at 18:12
  • Thank you very much. Now I figured out what you mean. I solved the problem. – Selçuk Özübek Jan 26 '21 at 18:15
  • @Olivier Rogier And thank you also for your advice on "goto". – Selçuk Özübek Jan 26 '21 at 18:18
  • Hi @SelçukÖzübek and welcome. It is considered polite to mark the answer that solved your problem as the answer that solved it. Normally you should see a ✔ left to the every answer. Clicking that ✔ will mark the answer as the one that solved your problem and will mark this answer as solved. – Ken Bonny Jan 27 '21 at 08:12
  • @KenBonny; i clicked that button yesterday, but it was not accepted. – Selçuk Özübek Jan 27 '21 at 11:24
  • @KenBonny; and i also can't vote. – Selçuk Özübek Jan 27 '21 at 11:25
  • It's not the up arrow. It looks like a check: ✔. I'm not sure if you need a certain amount of reputation to be able to vote. Since you are new, I thought I'd let you know. It kind of bugs me that there are numerous questions with valid answers that do not have the answered validation. Don't worry too much about it, it's one of my pet peeves. – Ken Bonny Jan 27 '21 at 12:59

2 Answers2

4

You got this compile error because this line is never executed:

int hak = 3;

It is because of the GOTOs, the position of the LABELS and the test condition if...else above this line: you never pass this line.

To solve this compile error as well as the design problem which is thus generated, you need to refactor all that to remove all the GOTOs.

Indeed, when compiling, before talking about executing, the if ( komut == '1' ) ... else ..., the hak creation in never reached, going to GIRIS or END.

Thus after when you try to use hak, it never was assigned and the compiler know that and tell you that.

Just to get to compile, you need to write:

GIRIS:
int hak = 3;

But using GOTOs is trashy and not clean, a very bad smell, as well as being source of mistakes and weird behaviors.

enter image description here

Avoid goto more than one once per century, and use refactoring instead, please.

GOTO still considered harmful?

Spaghetti code

Example of a minimal solution using a local method

if ( komut == '1' )
{
  int hak = 3;
  GIRIS(ref hak);
}
else
{
  Console.Write("Çıkış yaptınız. İyi günler dileriz.");
}

Console.ReadKey();

void GIRIS(ref int hak)
{
  Console.Write("Lütfen Kullanıcı Adınızı Giriniz: ");
  string kullanici_adi_giris = Console.ReadLine();
  Console.Write("Lütfen belirlemiş olduğunuz Şifrenizi giriniz: ");
  string sifre_giris = Console.ReadLine();

  int karsilastirma_k_adi = String.Compare(kullanici_adi, kullanici_adi_giris);
  int karsilastirma_sifre = String.Compare(sifre, sifre_giris);

  if ( karsilastirma_k_adi == 0 && karsilastirma_sifre == 0 )
  {
    Console.Write("Başarıyla Giriş Yaptınız. Hoşgeldiniz.");
  }
  else if ( hak == 0 )
  {
    Console.Write("3 defa yanlış giriş yaptınız. Müşteri hizmetleri temsilcisiyle görüşünüz. İyi günler dileriz.");
  }
  else
  {
    hak--;
    Console.WriteLine("Hatalı Giriş Yaptınız. Lütfen tekrar deneyiniz. {0} hakkınız kalmıştır.", hak);
    GIRIS(ref hak);
  }
}
1

The compiler is seeing that the line of code that will define a value for the variable hak is before the label GIRIS. It cannot be guaranteed that the line will be executed, because GIRIS could be reached by the line of code goto GIRIS.

Try changing this:

int hak = 3;
GIRIS:

to this:

GIRIS:
int hak = 3;
Nate W
  • 275
  • 2
  • 13