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();
}
You got this compile error because this line is never executed:
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 ...
, thehak
creation in never reached, going toGIRIS
orEND
.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:
But using GOTOs is trashy and not clean, a very bad smell, as well as being source of mistakes and weird behaviors.
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