Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to call method after pressing enter key in c#

I want to sum two numbers.
After entering the second number i will show "Please confirm the values by pressing Enter key or press Backspace key to reenter the values again". If the user pressed enter I want to execute the method. I also want that instead if user presses Backspace I want to repeat the task.

I’m a beginner in c#

ignore the comments in code

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

using System;  
namespace FunctionExample  
{  
    class Program  
    {  
        // User defined function without return type  
        public int Add(int firstNum,int secondNum) // No Parameter  
        {  
           
         return firstNum + secondNum;
           
        }  
        // Main function, execution entry point of the program  
        static void Main(string[] args)  
        {  
            Console.WriteLine("Please add first number");
            int num1 = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Please enter second num");
            int num2 = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Please Confirm the values by pressing Enter key or press Backspace to reenter the values");
            Program p = new Program();
            int sum = p.Add(num1,num2);
            Console.WriteLine("Total = ",sum);
            
            
            
                        
        }  
    }  
}

and please don’t tell me to remove the Add() method and just do that addition in main class , its a task so i have to do it like that

>Solution :

Please review below code.

static void Main(string[] args)
{
    try
    {
        while (true)
        {
            Console.Write("Please add first number (Numemic value only): ");
            int num1 = Convert.ToInt32(Console.ReadLine());
            Console.Write(Environment.NewLine + "Please enter second num (Numemic value only): ");
            int num2 = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine(Environment.NewLine + "Please Confirm the values by pressing Enter key or press Backspace to reenter the values");
            ConsoleKeyInfo consoleKeyInfo = Console.ReadKey();
            if (consoleKeyInfo.KeyChar == 13)
            {
                Console.WriteLine(Environment.NewLine + "Sum of " + num1 + " and " + num2 + " is: " + Add(num1, num2));
                Console.WriteLine(Environment.NewLine + "Press any key to exit.");
                Console.ReadKey();
                break;
            }
            else if (consoleKeyInfo.KeyChar == 27)
                Console.Clear();
            else
                break;
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine("EXCEPTION" + Environment.NewLine + Environment.NewLine + "Exception Message - " + ex.Message + Environment.NewLine + Environment.NewLine + "Exception Stack Trace - " + ex.StackTrace + Environment.NewLine + Environment.NewLine + "Inner Exception Message - " + (ex.InnerException != null ? ex.InnerException.Message : "N/A") + Environment.NewLine + Environment.NewLine + "Inner Exception Stack Trace - " + (ex.InnerException != null ? ex.InnerException.StackTrace : "N/A") + Environment.NewLine + Environment.NewLine + "Base Exception Message - " + (ex.GetBaseException() != null ? ex.GetBaseException().Message : "N/A") + Environment.NewLine + Environment.NewLine + "Base Exception Stack Trace - " + (ex.GetBaseException() != null ? ex.GetBaseException().StackTrace : "N/A"), "EXCEPTION");
        Console.ReadKey();
    }
}

static int Add(int firstNum, int secondNum) // No Parameter  
{
    return firstNum + secondNum;
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading