I get an infinite loop when i input 1 and 5 in this program. Please help me detect the error.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Graphs
{
class Program
{
static void Main(string[] args)
{
string[] cities = new string[10] { "Makati City", "Taguig City", "Imus City", "Dasmariñas City", "Tagaytay City", "Parañaque City", "Muntinlupa City", "Sta. Rosa City", "Calamba City", "Tanauan City" };
int[,] mapGraph = new int[10, 10] { {0,1,0,0,0,0,0,0,0,0 },
{1,0,1,0,0,1,0,0,0,0 },
{0,1,0,1,0,0,0,0,0,0 },
{0,0,1,0,0,0,0,0,0,0 },
{0,0,0,1,0,0,0,1,0,0 },
{0,1,0,0,0,0,1,0,0,0 },
{0,0,0,0,0,1,0,1,0,0 },
{0,0,0,0,1,0,1,0,1,0 },
{0,0,0,0,0,0,0,1,0,1 },
{0,0,0,0,0,0,0,0,1,0 } };
int source = 0, destinaton = 0, row, column;
string source1, destination1, yn;
Console.SetWindowSize(70, 55);
Console.WriteLine("***********Somehere in the Philippines********************");
Console.WriteLine("*** 1.Makati City ***");
Console.WriteLine("*** // ***");
Console.WriteLine("*** 2.Taguig City ***");
Console.WriteLine("*** // \\ ***");
Console.WriteLine("*** 3.Imus City 6.Paranaque City ***");
Console.WriteLine("*** // \\ ***");
Console.WriteLine("*** // \\ ***");
Console.WriteLine("*** 4.Dasmarinas City 7.Muntinlupa City ***");
Console.WriteLine("*** // \\ ***");
Console.WriteLine("***5.Tagaytay City=====8.Sta. Rosa City ***");
Console.WriteLine("*** \\ ***");
Console.WriteLine("*** 9.Calamba City ***");
Console.WriteLine("*** \\ ***");
Console.WriteLine("*** 10.Tanauan City ***");
Console.WriteLine("**********************************************************");
Console.WriteLine("++====================List Of Cities=====================++");
try
{
do
{
for (int i = 0; i < 10; i++) //display's the list of cities
{
Console.WriteLine("[{0}] - {1}", i + 1, cities[i]);
}
Console.WriteLine("++======================================================++\n");
Console.WriteLine("\n\tEnter your first location: ");
source1 = Console.ReadLine();
//validates if the input is a number
while (source1 != "1" && source1 != "2" && source1 != "3" && source1 != "4" && source1 != "5" && source1 != "6" && source1 != "7" && source1 != "8" && source1 != "9" && source1 != "10")
{
Console.WriteLine("\tPlease Enter a valid number: ");
source1 = Console.ReadLine();
}
source = int.Parse(source1);
while (source > 10 || source < 1) //validates if the user entered the valid numbers frm 1-10in source
{
Console.WriteLine("\tPlease Enter Ranges<1-10>");
source1 = Console.ReadLine();
source = int.Parse(source1);
}
source -= 1;
Console.WriteLine("\tEnter your next location: ");
destination1 = Console.ReadLine();
//validates if the input is a number
while (destination1 != "1" && destination1 != "2" && destination1 != "3" && destination1 != "4" && destination1 != "5" && destination1 != "6" && destination1 != "7" && destination1 != "8" && destination1 != "9" && destination1 != "10")
{
Console.WriteLine("\tPlease Enter a valid number: ");
destination1 = Console.ReadLine();
}
destinaton = int.Parse(destination1);
while (destinaton > 10 || destinaton < 1)
{
Console.WriteLine("\tPlease Enter Ranges<1-10>");//validates if the user entered the valid numbers frm 1-10in destinaton
destination1 = Console.ReadLine();
destinaton = int.Parse(source1);
}
destinaton -= 1;
Console.WriteLine("\t++========================================================++");
Console.WriteLine("\t++====Source: {0} Destination: {1}=====++", cities[source], cities[destinaton]);
Console.WriteLine("\t++========================================================++");
Console.WriteLine("\tHere are the path/s to reach your desired location/s: ");
//Determines the path when source1 is connected away to destination1
if (mapGraph[source, destinaton] == 0)
{
if (source > destinaton)//reads from right to left
{
int count = 1;
for (row = source; row >= destinaton; row--)
{
column = 0;
while (mapGraph[row, column] != 1) column++;
Console.WriteLine("\t\t{0} - {1}", count++, cities[row]);
if (row != 0)
{
row = column + 1;
}
}
}
else if (source < destinaton)//reads from left to right
{
int count = 1;
for (row = source; row < destinaton; row++)
{
column = 9;
while (column > destinaton || mapGraph[row, column] != 1) column--;
Console.WriteLine("\t\t{0} - {1}", count++, cities[row]);
if (row != 9)
{
row = column - 1;
}
}
Console.WriteLine("\t\t{0} - {1}", count++, cities[row]);
}
else if (source == destinaton)
{
Console.WriteLine("\nSame Location. \"{0}\"", cities[source]);
}
}
//Determines the path of the location when source1 is connected directly to destination1
else if (mapGraph[source, destinaton] == 1)
{
Console.WriteLine("\t\t1 - {0}", cities[source]);
Console.WriteLine("\t\t2 - {0}", cities[destinaton]);
}
//ask user to search again
Console.WriteLine("\nSearch Again? [y/n]");
yn = Console.ReadLine();
while (yn != "Y" && yn != "y" && yn != "n" && yn != "N")
{
Console.WriteLine("Please Enter a Valid Input[y/n]");
yn = Console.ReadLine();
}
if (yn == "Y" || yn == "y")
{
Console.Clear();
}
} while (yn == "Y" || yn == "y");
Console.Clear();
Console.WriteLine("\n\n\n\n\t\t\tThank You and GoodBye!");
}
catch(System.Exception e)
{
Console.WriteLine(e);
}
}
}
}