locked
Multiply 2 numbers but add a condition to it? RRS feed

  • Question

  • Hello,

    I have a quite simple question(new to all of this).

    I've made a console program that multiplies two user input numbers, here's the code :

                Console.WriteLine("Enter the first number");
                var first_number = decimal.Parse(Console.ReadLine());

                Console.WriteLine("Enter the second number");
                var second_number = decimal.Parse(Console.ReadLine());

                Console.WriteLine("Answer:\n");
                Console.WriteLine(first_number  * second_number);

    My question is, how can I make a condition that the user can't use anything else than numbers?

    Thanks.

    Sunday, February 9, 2020 5:16 PM

All replies

  • Keeping it simple

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApp1
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.Write("enter first number ");
    
                decimal firstNumber = 0;
                decimal secondtNumber = 0;
    
                while (!decimal.TryParse(Console.ReadLine(), out firstNumber))
                {
                    Console.Clear();
                    Console.WriteLine("You entered an invalid number");
                    Console.Write("enter number of conversations ");
                }
    
                Console.Write("enter second number ");
                while (!decimal.TryParse(Console.ReadLine(), out secondtNumber))
                {
                    Console.Clear();
                    Console.WriteLine("You entered an invalid number");
                    Console.Write("enter number of conversations ");
                }
    
    
                Console.WriteLine($"{firstNumber * secondtNumber}");
                Console.ReadLine();
    
            }
        }
    }


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange


    Sunday, February 9, 2020 8:29 PM
  • Hi Zivanovic,

    Thank you for posting here.

    In C#, numeric types provide tryParse methods.

                bool bool1 = decimal.TryParse(Console.ReadLine(), out decimal value);
                if (bool1)
                {
                    Console.WriteLine(value);
                }
                else
                {
                    Console.WriteLine("It's not a number.");
                }
                Console.ReadLine();

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, February 10, 2020 7:03 AM