0

I just written this code and got an error from it. After a few minutes of googling I can't seem to find the answer. Here is my code:

using System;

namespace tugas1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Title = "Layanan Pet Hotel / Pet Grooming";

            int kategoriHewan, ukuranHewan, layananHewan, layananPetHotel;
            string jumlahHewan, namaLayananPetHotel;
            double harga = 0;

            Console.WriteLine("==============================================");
            Console.WriteLine("Tolong masukkan kategori: ");
            Console.WriteLine("[1] Dog");
            Console.WriteLine("[2] Cat");
            Console.WriteLine("==============================================");
            Console.Write("Masukkan kategori hewan [1/2]: ");
            kategoriHewan = int.Parse(Console.ReadLine());
            if (kategoriHewan == 1)
            {
                Console.Clear();
                Console.WriteLine("==============================================");
                Console.WriteLine("Tolong masukkan ukuran: ");
                Console.WriteLine("[1] Besar");
                Console.WriteLine("[2] Sedang");
                Console.WriteLine("[3] Kecil");
                Console.WriteLine("==============================================");
                Console.Write("Masukkan ukuran: [1/2/3]");
                ukuranHewan = int.Parse(Console.ReadLine());
            }
            else if (kategoriHewan == 2)
            {
                Console.WriteLine("Untuk kategori Cat tidak memiliki ukuran.");
            }
            Console.Clear();
            Console.Write("Masukkan jumlah hewan: ");
            jumlahHewan = Console.ReadLine();
            Console.WriteLine("==============================================");
            Console.WriteLine("Tolong masukkan kategori layanan: ");
            Console.WriteLine("[1] Layanan Pet Hotel");
            Console.WriteLine("[2] Layanan Pet Grooming");
            Console.WriteLine("==============================================");
            Console.Write("Masukkan kategori hewan [1/2]: ");
            layananHewan = int.Parse(Console.ReadLine());
            if (kategoriHewan == 1)
            {
                if (layananHewan == 1)
                {
                    if (ukuranHewan == 1)
                    {
                        Console.Clear();
                        Console.WriteLine("==============================================");
                        Console.WriteLine("[1] Suite Room + Meal             (Rp. 110.000,-)");
                        Console.WriteLine("[2] Suite Room + No Meal          (Rp. 105.000,-)");
                        Console.WriteLine("[3] Standard Room + Meal          (Rp. 100.000,-)");
                        Console.WriteLine("[4] Standard Room + No Meal       (Rp.  95.000,-)");
                        Console.WriteLine("==============================================");
                        Console.Write("Masukkan Layanan Pet Hotel untuk kategori Dog [1/2/3/4]: ");
                        layananPetHotel = int.Parse(Console.ReadLine());

                        switch (layananPetHotel)
                        {
                            case 1:
                                harga = 110000;
                                namaLayananPetHotel = "Suite Room + Meal";
                                break;
                            case 2:
                                harga = 105000;
                                namaLayananPetHotel = "Suite Room + No Meal";
                                break;
                            case 3:
                                harga = 100000;
                                namaLayananPetHotel = "Standard Room + Meal";
                                break;
                            case 4:
                                harga = 95000;
                                namaLayananPetHotel = "Standard Room + No Meal";
                                break;
                        }
                    }
                }
            }

        }
    }
}

I got these error:

Use of unassigned local variable 'ukuranHewan' The variable 'namaLayananPetHotel' is assigned but its value is never used The variable 'harga' is assigned but its value is never used

Roy
  • 1
  • Welcome to SO. ```ukuranHewan``` is defined only if ```kategoriHewan == 1```, otherwise it isn't defined. As for the other two, you define those variables but you don't use them. – ewokx Apr 11 '22 at 04:23
  • give a default value when declare it. for example, `int ukuranHewan = 0;` – Lei Yang Apr 11 '22 at 04:23
  • Further to @ewong comment, the error arises because you refer later in ukuranHewan in an `if`. The compiler complains, because it cannot be sure at runtime, where it will have a value at this point. – Jonathan Willcock Apr 11 '22 at 04:28
  • 1
    "...value is never used" isn't normally an *error* by the way, Otis just a warning indicator that you might have forgotten to write some code or might have forgotten to remove some code – Caius Jard Apr 11 '22 at 04:49
  • thanks everyone! now i'm left with the warning which is 'namaLayananPetHotel' and 'harga' which confuses me a lil bit – Roy Apr 11 '22 at 04:51

1 Answers1

0

Analysis

The reason why "ukuranHewan" is showing unassigned because

if (kategoriHewan == 1)

if the above condition is false, the value for ukuranHewan is not assigned.

if (ukuranHewan == 1)

but here you are using irrespective of the value is assigned or not.

"namaLayananPetHotel" is assigned but never used. because in the switch statement you are assigning the value but you are not displaying anywhere ,same reason for harga also.