3
Reply

C# user entered value in array compare another entered value

Mark Blankenburg

Mark Blankenburg

Aug 6 2013 11:49 AM
3k
Hello,
I am new to learning C# and am self teaching myself, this is not for homework but I want to be able to understand this before I move on in my learning/studies.

Here is my task:
Ask the user to enter 10 numbers and store these in a one dimensional array. Get one additional number from the user and then search the array to see if any of the elements is equal to this additional number. If you find it, display a message that you did and if not display another one that it could not be found.


Here is what I have thus far:

namespace CH7Challenge1
{
    class Program
    {
        static void Main(string[] args)
            /*
             * Ask the user to enter 10 numbers and store these in a one dimensional array. 
             * Get one additional number from the user and then search the array to see if any of the elements is equal to this additional number. 
             * If you find it, display a message that you did and if not display another one that it could not be found.
             * */
            
        {

            int[] myNums;
            myNums = new int[10];
            int oneNum;

                  
            //Get the ten numbers from the user
            Console.WriteLine("Please enter 10 numbers: ");
            for (int i = 0; i < 10; i++)
            {
                Console.Write("Number {0}: ", i + 1);
                myNums[i] = int.Parse(Console.ReadLine());
                               
            }
            
            //get one additonal number from user
            Console.WriteLine("please enter one more number:");
            oneNum = int.Parse(Console.ReadLine());



My thought next was to just use an if statement like:
if(myNums[i]==oneNum)
{
Console.WriteLine("Your single number matches a number in the array");
}
else
Console.WriteLine("No match");


However this does not work because i cannot use the user entered value stored in [i] outside of the loop...so I am kind of stumped on how to make this work based on my knowledge thus far.

I appreciate any help from anyone.

Answers (3)