none
Stopping float form being string or checking it? RRS feed

  • Question

  • float Input = float.Parse(Console.ReadLine());

    if (Input>0)

    {

       Console.WriteLine("Well done!")

    }

    else

    Console.WriteLine("Wrong!")

    }

    How do I either stop a user from entering characters or give an error to them without the program stopping.

    Sorry I am a start.

    Friday, October 11, 2019 8:13 PM

All replies

  • Try the following which is a modified version of this.

    using System;
    namespace ConsoleApp1
    {
        class Program
        {
            static void Main(string[] args)
            {
                string stringValue = "";
                Console.Write("Enter your value: ");
                ConsoleKeyInfo key;
    
                do
                {
    
                    key = Console.ReadKey(true);
    
                    if (key.Key != ConsoleKey.Backspace)
                    {
                        float doubleValue = 0; 
                        var parseResult = float.TryParse(key.KeyChar.ToString(), out doubleValue); 
                        if (parseResult)
                        {
                            stringValue += key.KeyChar;
                            Console.Write(key.KeyChar);
                        }
                        else
                        {
                            if (key.KeyChar != '.') continue;
                            if (!stringValue.Contains("."))
                            {
                                stringValue += key.KeyChar;
                                Console.Write(key.KeyChar);
                            }
    
                        }
                    }
                    else
                    {
                        if (key.Key == ConsoleKey.Backspace && stringValue.Length > 0)
                        {
                            stringValue = stringValue.Substring(0, (stringValue.Length - 1));
                            Console.Write("\b \b");
                        }
                    }
                }
                // Stops Receiving Keys Once Enter is Pressed
                while (key.Key != ConsoleKey.Enter);
    
                Console.WriteLine();
                Console.WriteLine("The Value You entered is : " + stringValue);
                Console.ReadKey();
            }
        }
    
    
    }
    


    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

    Friday, October 11, 2019 10:26 PM
    Moderator
  • Also try a simplified approach like this:

    float Input;

    bool is_number;

     

    do

    {

       is_number = float.TryParse( Console.ReadLine( ), out Input );

       if( !is_number )

       {

          Console.WriteLine( "Bad value! Please re-enter." );

       }

    } while( !is_number );

     

    if( Input > 0 )

    {

       Console.WriteLine( "Well done!" );

    }

    else

    {

       Console.WriteLine( "Wrong!" );

    }

     

    It is possible to repeat the loop for negative numbers as well.

    Saturday, October 12, 2019 8:19 AM
  • Hi Ottoisking,

    Thank you for posting here.

    For your question, you program will stop when user entering characters.

    In fact, you can replace float.Parse with float.TryParse. 

    It will judge the type of data the user enters.

    You could try the following code to get it.

                while (true)
                {
                    bool isValue = float.TryParse(Console.ReadLine(), out float input);
                    if (!isValue)
                    {
                        Console.WriteLine("Please enter a number.");
                    }
                    else
                    {
                        if (input > 0)
                        {
                            Console.WriteLine("Well done!");
                        }
                        else
                        {
                            Console.WriteLine("Wrong!");
                        }
                    }
                }

    Result:


    Hope my solution could be helpful.

    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, October 14, 2019 2:19 AM