0

This is only a small piece of the code I'm trying to make work. The original code I wrote works but I want to make sure when the user inputs a number it is in fact a number.

Console.WriteLine("Give the number of A");
A =Convert.ToDouble( Console.ReadLine());

if (char.IsNumber(Convert.ToDouble(A)) == correct)
{
    Console.WriteLine(Convert.ToDouble( A * A));  
   
}
else
{
    Console.WriteLine("Incorrecrt input");
}
           

The Console.WriteLine(Convert.ToDouble(A*A)); I only wrote to see if that will work and it doesn't. After the user inputs only a number I must use it in another equation for a final answer. The user must input 3 numbers.

9
  • Which way it doesn't work? Any exception, or difference between expected and taken?
    – Renat
    Commented Sep 30, 2020 at 10:27
  • A seems to be already double. Result of A * A doesn't need conversion.
    – Sinatr
    Commented Sep 30, 2020 at 10:28
  • 2
    Use TryParse, anyway this is 1 of a million September duplicates
    – TheGeneral
    Commented Sep 30, 2020 at 10:29
  • when I run it shut down, and the message says cannot convert double to char
    – Frederick
    Commented Sep 30, 2020 at 10:29
  • 1
    char.IsNumber(Convert.ToDouble(A)) doesn't make sense.. at all.
    – Sinatr
    Commented Sep 30, 2020 at 10:29

2 Answers 2

2

For me, you should check is the input is a number that you can convert to double before converting it.

       Console.WriteLine("Give the number of A");
       
        var a = Console.ReadLine();
        double number;
        if (double.TryParse(a, out number))//double.TryParse takes a parameter and tries to convert it to double. If the convertion is successfull, sets the out parameter as result and returns true. If not, returns false. And you can use with other types like int.Tryparse(param, out outParam);
        {
            A = number;
        }
        else
        {
          //Not a number, show a message etc...
        {
3
  • You can inline the number declaration with TryParse(a, out double number), which is obviously shorter but also means you can't accidentally use it outside the if block before it's assigned a value.
    – WSC
    Commented Sep 30, 2020 at 11:27
  • Why do you use var a? And I never used this before if (double.TryParse(a, out number)), can you maybe explain it, please.
    – Frederick
    Commented Sep 30, 2020 at 11:45
  • var is an implicit variable declaration. It basically means "this variable is the same type as whatever gets assigned to it". In this case, Console.ReadLine() always returns a string so var a is the same as string a. You should read up about TryParse: learn.microsoft.com/en-us/dotnet/api/…
    – WSC
    Commented Sep 30, 2020 at 12:34
2

If you break this down:

A =Convert.ToDouble( Console.ReadLine());
if (char.IsNumber(Convert.ToDouble(A)) == correct)
{

}

What you're basically doing is:

Double A = Convert.ToDouble(Console.ReadLine());
Double dbl = Convert.ToDouble(A);
Boolean bl = char.IsNumber(dbl);

if (bl== correct)
{

}

So there's multiple things wrong here.

Firstly, you're trying to convert the user input without any sort of guarantee of success, so if someone typed "A" instead of a number it will throw an exception.

You should use TryParse and then if it's a valid conversion proceed.

Secondly, you're checking if a Double is a char that can be converted to a number. Obviously not because it's already a Double.

Thirdly, you're checking if a Boolean is equal to some variable called correct (which you haven't provided the definition of) so it's not clear if this is a valid comparsion.

EDIT:

This is what I would do:

bool validInput = false;
do
{
    Console.WriteLine("Give the number of A");
    string userInput = Console.ReadLine();

    if (double.TryParse(userInput, out double result))
    {
        validInput = true;
        Console.WriteLine(result * result);
        Console.ReadLine();
    }
    else
    {
        Console.WriteLine("Invalid input. Please type a number.");
    }
} while (!validInput);
3
  • Thanks for the clarification. All I want to do is, the user must give me a number actually three but only one for now. Then check if it is a number, if it is then it can be used it the next step where it will be used to calculate something.
    – Frederick
    Commented Sep 30, 2020 at 10:51
  • @Frederick Have a look into Coskun's answer. The comination of both answers is key to you. This one to understand your problem and Coskun's for what is the solution...
    – Fildor
    Commented Sep 30, 2020 at 11:03
  • @Frederick I've added an example solution which will do what you're attempting, but I would strongly encourage you to understand why your code wasn't working rather than just copying and pasting this code. :)
    – WSC
    Commented Sep 30, 2020 at 11:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.