user3263971
user3263971

Reputation: 63

Writing double in console C#

I need your help about C#. I'm creating this program:

static void Main()
    {
        Console.WriteLine("You weight is: ");
        double weight = Double.Parse(Console.ReadLine());
        Console.WriteLine("Your weight on Moon is: + {0}",weight*0.17);
     }

And the problem is, when I start the program and input, for example 35.9, a number for the weight, the program crashes.

Upvotes: 2

Views: 434

Answers (4)

user3263971
user3263971

Reputation: 63

Thanks for all of your help. I found out that in my general computer settings i have chosen comma as a decimal symbol and when i wrote in the console i used full stop. So this is the reason why it closes. :)

Upvotes: 1

arpitbakshi
arpitbakshi

Reputation: 644

Try this code it'll definitely work:

Console.WriteLine("You weight is: ");
double weight;
double.TryParse(Console.ReadLine(), out weight);
Console.WriteLine("Your weight on Moon is: + {0}", weight * 0.17);

Upvotes: 0

Liam McInroy
Liam McInroy

Reputation: 4356

If you put breakpoints through the program, you can see it writes to the console, you just don't get an opportunity to see it.

Wait for a key using this:

Console.WriteLine("Press any key to exit");
ConsoleKeyInfo c = Console.ReadKey();

Upvotes: 1

Grant Winney
Grant Winney

Reputation: 66439

It doesn't crash. It closes.

You need to place Console.ReadLine() at the end of the method if you want the window to stay open long enough to read the message.

static void Main()
{
    Console.WriteLine("You weight is: ");
    double weight = Double.Parse(Console.ReadLine());
    Console.WriteLine("Your weight on Moon is: + {0}",weight*0.17);

    Console.ReadLine();  // waits for input, allowing you to read the message
 }

Upvotes: 5

Related Questions