Jonatan Johansson
Jonatan Johansson

Reputation: 3

Why are my vectors being reset instead of having their own value?

I'm studying programing right now and I need to make a "bus". I've got a vector with 25 spots and user will fill these with passengers. I've got a switch-case menu so you can choose if you want to add a passenger or get the full passenger list and so on. When I've entered about 5 passengers and want to get the passenger list everyone on the bus turns to 0 instead of the age I entered. I have no idea what's wrong?

Comments in the code are in Swedish.

{
    class Buss
    {
        public int[] passagerare;
        public int antal_passagerare ;


        public void Run()
        {
            Console.WriteLine("Welcome to the awesome Buss-simulator");

            do
            {
                Console.WriteLine("Välj alternativ");
                Console.WriteLine("1: Lägg till passagerare");
                Console.WriteLine("2: Skriv ut listan över passagerare");
                Console.WriteLine("3: Skriv ut total åldern över passagerna");
                Console.WriteLine("4: Skriv ut medelåldern över passagerarna");
                Console.WriteLine("0: Avsluta programmet.");

                string str = Console.ReadLine();
                int temp = Convert.ToInt32(str);



                switch (temp)
                {
                    case 1:
                        Console.WriteLine("Lägg till passagerare (ange ålder endast)!");
                        add_passenger();
                        break;
                    case 2:
                        Console.WriteLine("Skriv ut gästlistan!");
                        print_buss();
                        break;
                    case 3:
                        Console.WriteLine("hejsan");
                        break;
                    case 4:
                        Console.WriteLine("hejsan");
                        break;
                    case 0:
                        Console.WriteLine("hejsan");
                        break;
                }



                //ska ändra så att om man väljer 0 så stängs programmet

            } while (true);


            //Här ska menyn ligga för att göra saker
            //Jag rekommenderar switch och case här
            //I filmen nummer 1 för slutprojektet så skapar jag en meny på detta sätt.
            //Dessutom visar jag hur man anropar metoderna nedan via menyn
            //Börja nu med att köra koden för att se att det fungerar innan ni sätter igång med menyn.
            //Bygg sedan steg-för-steg och testkör koden.
        }

        //Metoder för betyget E

        public void add_passenger()
        {




            passagerare = new int[25];

            if (antal_passagerare < 25)

            {
                Console.WriteLine("Ålder på passagerare nr " + antal_passagerare);
                string age = Console.ReadLine();
                int age2 = Convert.ToInt32(age);
                passagerare[antal_passagerare] = age2;
                Console.WriteLine(passagerare[antal_passagerare]);
                antal_passagerare++;
            }

            else
            {
                Console.WriteLine("Bussen är full!");
            }
            //Lägg till passagerare. Här skriver man då in ålder men eventuell annan information.
            //Om bussen är full kan inte någon passagerare stiga på
        }

        public void print_buss()
        {
            for (int i = 0; i < antal_passagerare; i++)
            {
                Console.WriteLine(passagerare[i]);
            }

        }

        // public int calc_total_age()
        //{

        //}


        //public int calc_average_age()
        //{

        //}



        public void find_age()
        {

        }

        public void sort_buss()
        {

        }



    }

    class Program
    {
        public static void Main(string[] args)
        {
            var minbuss = new Buss();
            minbuss.Run();
            Console.Write("Press any key to continue . . . ");
            Console.ReadKey(true);
        }
    }
}

Upvotes: 0

Views: 163

Answers (1)

JamesS
JamesS

Reputation: 2300

If I am understanding this correctly, you are setting the array:

passagerare = new int[25];

to a new array every time you get into the add_passenger function so altthough you may actually set it the first time, the next time the user gets into the function, it'll be reset again.

If this is the case, the you can simply define it before you call this function and pass it as a parameter.

EDIT: In regards to your comment, you can define it in one of two palces.

1) You can create a constructor that sets the array to a length of [25] such as:

public Buss(){
    passagerare = new int[25];
}

2) You can define it before you enter your switch statement and pass the array into to each function as a parameter.

Upvotes: 3

Related Questions