0

I am trying to create a multi functional program. For one part, an array is filled with random numbers. The user then enters in a number and the program returns which positions the number appears in the array. It also returns the number of times the number appears in the array.

However it only does this once and after that it ends the program. I want it to prompt the user to enter in a number to search until the user presses a button, let's say 'P' for example. Once the user presses 'P' after the results are shown, the program should close. Any tips into what methods or functionality I should be using?

Here is a broken down version of my code.

Console.Write("Now enter a number to compare: ");
int c = Convert.ToInt32(Console.ReadLine());

for (int j = 0; j < arr.Length; j++)
{
    if (arr[j] == c)
    {
        pos.Add(j);
    }
}          

if (pos.Count == 0)
{
    Console.WriteLine("Sorry this number does not match");
}
else
{
   Console.WriteLine("The number {0} appears {1} time(s)",c,pos.Count);
}

Console.ReadLine();
Christos
  • 53,228
  • 8
  • 76
  • 108
Theman
  • 231
  • 3
  • 16
  • possible duplicate of [C# Make Program Wait For Button To Be Pressed](http://stackoverflow.com/questions/5628274/c-sharp-make-program-wait-for-button-to-be-pressed) – Baximilian Jul 21 '14 at 14:37
  • 3
    Hint: Look up `while` loops. – D Stanley Jul 21 '14 at 14:37
  • The problem definition and the question are both unclear. Please try to clearly re-define your problem and narrow down the question. Regards, – Alexander Bell Jul 21 '14 at 14:38
  • 2
    You seem to grasp the concept of loops (I see one already in your code). Think about where else you can apply a loop. – Michael Jul 21 '14 at 14:38

4 Answers4

0

this should give you a little head start

you have to use a loop around your code and check for a keyword to exit

class Program
{
    static void Main(string[] args)
    {
        var arr = new int[50];
        var pos = new List<int>();
        string result;
        do
        {
            Console.Write("Now enter a number to compare: ");
            result = Console.ReadLine();

            int c;

            if (int.TryParse(result, out c))
            {
                for (int j = 0; j < arr.Length; j++)
                {
                    if (arr[j] == c)
                    {
                        pos.Add(j);
                    }
                }

                if (pos.Count == 0)
                {
                    Console.WriteLine("Sorry this number does not match");
                }
                else
                {
                    Console.WriteLine("The number {0} appears {1} time(s)", c, pos.Count);
                }
            }


        } while (result != "exit");
    }
}
Fredou
  • 19,848
  • 10
  • 58
  • 113
0

I am going to provide another approach, didn't test the code below though.

class Program
{

    //declare your class variables here
    //so that you can access them from the methods and do your operations
    bool Up=true;

    static void Main(string[] args)
    {
        Console.WriteLine("Program started.");

        ThreadPool.QueueUserWorkItem(ConsoleCommands);
        while (Up)
        {
            Thread.Sleep(2000);
        }
        Console.WriteLine("Program ended.");
    }

    private static void ConsoleCommands(object dummy)
    {
        while (Up)
        {
            string cmd = ConsoleReceiver().ToLower();
            switch (cmd)
            {
                case "exit":
                    Up=false;
                    break;
                //implement more cases here and fill the rest of your business
                //example:
                case "1":
                    if (pos.Count == Int32.Parse(cmd))//just a dummy business
                    {
                        Console.WriteLine("Sorry this number does not match");
                    }
                    else//another dummy business
                    {
                        Console.WriteLine("Sth...");
                    }
                    break;
                default:
                    Console.WriteLine("Unrecognized command");
                    break;
            }//or forget about switch and use if-else stements instead.
        }
    }

    private static string ConsoleReceiver()
    {
        Console.WriteLine("#cmd:");
        return Console.ReadLine();
    }
}
Oğuz Sezer
  • 320
  • 3
  • 15
0

If you want to explicitly read single keys strokes...

ConsoleKeyInfo keyInfo;
do {
    Console.Write("Enter a number to compare; press the 'p' key to quit: ");
    keyInfo = Console.ReadKey(false);

    int c;
    if (Int32.TryParse(keyInfo.KeyChar.ToString(), out c))
    {    
        for (int j = 0; j < arr.Length; j++)
        {
            if (arr[j] == c)
            {
                pos.Add(j);
            }
        }          

        if (pos.Count == 0)
        {
            Console.WriteLine("Sorry this number does not match");
        }
        else
        {
           Console.WriteLine("The number {0} appears {1} time(s)",c,pos.Count);
        }
} while (keyInfo.Key != ConsoleKey.P)

Otherwise you can get creative with combinations of what @Fredou and I have posted.

Greg B
  • 416
  • 3
  • 12
-1

Try this :)

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            start:
            string tryagain;


           //All of your Code Goes here


            tryagain = Console.ReadLine();
            if (tryagain != "p")
            {
            goto start;    
            }

            else
            {
                Environment.Exit(0);
            }


        }
    }
}
Has9
  • 41
  • 1
  • 7