Hi! I want to ask if anyone can help me with my problem. I'm trying to write a program for an address book in C# using arrays.I'm still in the beginning and already have a problem.
This is the code i've written so far :
In the for loop when the program goes to this line
it breaks and shows me this message "Index was outside the bounds of the array."
I can't understand why it's happening and can't figure out a way to fix it.
I'll be happy for any help you can give me .
P.S. I'm a novice with C#.
This is the code i've written so far :
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Hope
{
static void Main(string[] args)
{
string str1, str2, str3, str4, str5;
Console.WriteLine("Enter Family name");
string s1 = Console.ReadLine();
Console.WriteLine("Enter first name");
string s2 = Console.ReadLine();
Console.WriteLine("Enter road number");
string s3 = Console.ReadLine();
Console.WriteLine("Enter road name");
string s4 = Console.ReadLine();
Console.WriteLine("Enter town name");
string s5 = Console.ReadLine();
string[,] addressBook = { { s1 }, { s2 }, { s3 }, { s4 }, { s5 } };
for (int i = 0; i < addressBook.Length / 2; i++)
{
str1 = addressBook[i, 0];
str2 = addressBook[i, 1];
str3 = addressBook[i, 2];
str4 = addressBook[i, 3];
str5 = addressBook[i, 4];
Console.WriteLine(" {0}, {1}, {2}, {3}, {4} ", str1, str2, str3, str4, str5);
Console.ReadLine();
}
Console.WriteLine();
}
}
}
Code:
str2 = addressBook[i, 1];
I can't understand why it's happening and can't figure out a way to fix it.
I'll be happy for any help you can give me .
P.S. I'm a novice with C#.
Comment