Reputation: 1
i wrote a simple code my program need two input one is name and 2th is number(phone number). and i wrote one section to find contacts. when user enter 1 he should add a name and phone number then user can enter 1or 2(1 for add contact and 2 for search) when user enter 1 again all of old date well delete and i don't want this please help.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace project_01
{
class Program
{
static void Main(string[] args)
{
string[] names = new string[5];
string[] phonenumb = new string[5];
bool itsTrue;
//int counter;
string help = @"enter 1 for add contact.
enter 2 for exit.0
enter 3 for search";
Console.WriteLine(help);
while (true)
{
Console.Write("=>");
int input = Convert.ToInt32(Console.ReadLine());
if (input == 1)
{
for (int i = 0; i < names.Length;)
{
Console.Write("name : ");
names[i] = Console.ReadLine();
Console.Write("number : ");
phonenumb[i] = Console.ReadLine();
i++;
break;
}
}
else if (input == 2)
{
Console.Write("enter y for exir and n to stay: ");
string exit = Console.ReadLine().ToUpper();
if (exit == "Y")
{
break;
}
else if (exit == "N")
{
continue;
}
}
else if (input == 3)
{
Console.Write("enter the name: ");
string search = Console.ReadLine();
itsTrue = false;
for (int j = 0; j < names.Length; j++)
{
if (search == names[j])
{
Console.WriteLine("name: {0}", names[j]);
Console.WriteLine("number:{0}", phonenumb[j]);
itsTrue = true;
break;
}
}
if (!itsTrue)
{
Console.WriteLine("content not found.");
}
}
}
}
}
}
Upvotes: 0
Views: 443
Reputation: 78840
Let's step through your code for menu option 1:
for (int i = 0; i < names.Length;)
{
Console.Write("name : ");
names[i] = Console.ReadLine();
Console.Write("number : ");
phonenumb[i] = Console.ReadLine();
i++;
break;
}
It starts off a loop with i
being set to 0 and loops while i < names.Length
. On the first iteration of the loop, it assigns names[i]
(which would be names[0]
) to a value read from the console and then the phone number. i
is then incremented to 1
, and the loop breaks right after.
Now imagine the next time 1
is pressed. It's still running the exact same code. The loop starts off with i
as 0, assigns to names[0]
and phonenumb[0]
, then breaks out of the loop. Not what you want.
Do you want a loop for adding a new row? If so, why? Is it doing something more than once? No. It's just supposed to do one thing. Note that even with your loop you're always breaking out of the loop on the first iteration, so it's not even a loop.
The missing piece is that you want to write to names
and phonenumb
at different indexes each time. To do this, you need to have a variable that persists through every iteration of the menu loop. If you declare something like an int insertIndex = 0;
outside of your menu loop, then you can change your insert logic to something like this instead:
Console.Write("name : ");
names[insertIndex] = Console.ReadLine();
Console.Write("number : ");
phonenumb[insertIndex] = Console.ReadLine();
insertIndex++;
Upvotes: 1