Sunday, November 30, 2014

C# Sequential Searches

Greetings!


I am currently working on a program that allows the user to enter a name of a soldier and their dog tag number with objects and array. I have it all done but the problem comes with searching the content with either a string or an integer. Do I have to make 2 arrays? One for the names and another for the numbers? I want my program to display: Dog tag#12345 Private Oliver found at number 2.


Right now I have this in my main method:



int arraySize = 3;
int index = 0;
int soldierTag;
string soldierName;
string searchValue;

Soldier[] soldierArray = new Soldier[arraySize];

do
{
Console.Write("Please enter a 3 number dog tag: ");
soldierTag= int.Parse(Console.ReadLine());

if (soldierTag < 100)
{
Console.WriteLine(" Error");

}
else
{
Console.Write("Enter name of soldier: ");
soldierName = Console.ReadLine();

index++;
}
} while (index < arraySize);

Console.Write("Look for a soldier name or dog tag number: ");
searchValue = Console.ReadLine();

This is a portion of my class called Soldier with objects:





// I have another one like this for soldierTag



public string soldierName
{
get
{
return AsoldierName;
}
set
{
AsoldierName = value;
}
}

public Student(string soldierName, int soldierTag)
{
AsoldierName = soldierName;
AsoldierTag = soldierTag;
}

I can't seem to figure out how to search inside it. Do I need to add an if statement, a loop or maybe something else? I'm quite new and I have absolutely no idea.



No comments:

Post a Comment