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.

Fildor
  • 14,510
  • 4
  • 35
  • 67
Frederick
  • 13
  • 1
  • Which way it doesn't work? Any exception, or difference between expected and taken? – Renat Sep 30 '20 at 10:27
  • `A` seems to be already `double`. Result of `A * A` doesn't need conversion. – Sinatr Sep 30 '20 at 10:28
  • 2
    Use TryParse, anyway this is 1 of a million September duplicates – TheGeneral Sep 30 '20 at 10:29
  • when I run it shut down, and the message says cannot convert double to char – Frederick Sep 30 '20 at 10:29
  • 1
    `char.IsNumber(Convert.ToDouble(A))` doesn't make sense.. at all. – Sinatr Sep 30 '20 at 10:29
  • char.IsNumber is only returning one character. Use Double.TryParse() – jdweng Sep 30 '20 at 10:30
  • Does this answer your question? [Converting string to double in C#](https://stackoverflow.com/questions/11399439/converting-string-to-double-in-c-sharp) – nilsK Sep 30 '20 at 11:00
  • Frederick: If something "doesn't work", it is always a good idea to include the error message (and in case of an exception: stacktrace) in the question. In this case, it's pretty obvious to us - but take it as a general advice on writing questions. – Fildor Sep 30 '20 at 11:02
  • There is no way this question should have survived, please do basic research before asking a question. This is of value to noone (not even you ) – TheGeneral Sep 30 '20 at 11:31

2 Answers2

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...
        {
Coskun Ozogul
  • 2,389
  • 1
  • 20
  • 32
  • 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 Sep 30 '20 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 Sep 30 '20 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`: https://learn.microsoft.com/en-us/dotnet/api/system.double.tryparse?view=netcore-3.1 – WSC Sep 30 '20 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);
WSC
  • 903
  • 10
  • 30
  • 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 Sep 30 '20 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 Sep 30 '20 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 Sep 30 '20 at 11:23