user13310388
user13310388

Reputation:

for loop keeps looping when there is a break

I'm trying to make a carpark program and one of the functions is to move a vehicle. It is working, the object is changing position in the array, it's just that the loop continues and it crashes because parkingLot[i, j].RegNummer is null the second time it loops through it. How do I stop it from looping again after I have "moved" a car? I want it to break and go back to the menu. (This is in a switch case connected to a menu)

Console.WriteLine("Enter the licensenumber of the vehicle that you want to move: ");
                        string lNumber = Console.ReadLine();
                        for (int i = 0; i < 100; i++)
                        {
                            for (int j = 0; j < 2; j++)
                            {
                                if (lNumber == parkingLot[i, j].RegNummer)
                                {
                                    Console.WriteLine("Enter the space you want to move it to: ");
                                    int space = int.Parse(Console.ReadLine());
                                    int space1 = space - 1;
                                    if (parkingLot[space1, 0] == null)
                                    {
                                        parkingLot[space1, 0] = parkingLot[i, j];
                                        parkingLot[i, j] = null;
                                        Console.WriteLine("The vehicle with licensenumber {0} has been moved to space {1}", lNumber, space);
                                        break;
                                    }
                                }
                                else
                                {
                                    Console.WriteLine("The vehicle with licensenumber {0} could not be found!", lNumber);
                                    break;
                                }
                            }
                        }

Upvotes: 1

Views: 135

Answers (2)

Ryan Wilson
Ryan Wilson

Reputation: 10765

Your break statements exit the inner for loop, if you need to exit the outer for loop, I'd suggest using a boolean, and checking if it's true to exit the outer loop. When you meet a condition on the inner for loop, set it to true:

 string lNumber = Console.ReadLine();
 bool exitOuter = false; //Use boolean to exit outer for loop
 for (int i = 0; i < 100; i++)
 {        
     for (int j = 0; j < 2; j++)
     {
         if (lNumber == parkingLot[i, j].RegNummer)
         {
             Console.WriteLine("Enter the space you want to move it to: ");
             int space = int.Parse(Console.ReadLine());
             int space1 = space - 1;
             if (parkingLot[space1, 0] == null)
             {
                 parkingLot[space1, 0] = parkingLot[i, j];
                 parkingLot[i, j] = null;
                 Console.WriteLine("The vehicle with licensenumber {0} has been moved to space {1}", lNumber, space);
                 exitOuter = true; //Set boolean to true to exit outer for loop
                 break;
              }
         }
         else
         {
              Console.WriteLine("The vehicle with licensenumber {0} could not be found!", lNumber);
              break;
         }
      }
      
      //Check boolean to break outer for loop
      if(exitOuter)
         break;
                
 }

Upvotes: 1

Buhhovsky
Buhhovsky

Reputation: 101

So, the problem is that you break just from the inner loop.

                                   if (parkingLot[space1, 0] == null)
                                {
                                    parkingLot[space1, 0] = parkingLot[i, j];
                                    parkingLot[i, j] = null;
                                    Console.WriteLine("The vehicle with licensenumber {0} has been moved to space {1}", lNumber, space);
                                    break;
                                }

You must inform the outer loop that the car has been parked and it must end the task. You can implement a flag for that.

        Console.WriteLine("Enter the licensenumber of the vehicle that you want to move: ");
        string lNumber = Console.ReadLine();
        bool carParked = false;
        for (int i = 0; i < 100; i++)
        {
            for (int j = 0; j < 2; j++)
            {
                if (lNumber == parkingLot[i, j].RegNummer)
                {
                    Console.WriteLine("Enter the space you want to move it to: ");
                    int space = int.Parse(Console.ReadLine());
                    int space1 = space - 1;
                    if (parkingLot[space1, 0] == null)
                    {
                        parkingLot[space1, 0] = parkingLot[i, j];
                        parkingLot[i, j] = null;
                        Console.WriteLine("The vehicle with licensenumber {0} has been moved to space {1}", lNumber, space);
                        carParked = true;
                        break;
                    }
                }
                else
                {
                    Console.WriteLine("The vehicle with licensenumber {0} could not be found!", lNumber);
                    break;
                }
            }
            if (carParked)
            {
                break;
            }
        }

Upvotes: 0

Related Questions