Question Reading in a value and converting to double

Grinder

New member
Joined
Mar 3, 2020
Messages
2
Programming Experience
Beginner
Hello everyone,

I'm a grown up man who's taking a part time course at a local University to gain more knowledge about programming. Since I'm in full job next to the course, I'm not able to attend the lectures, but I'm doing all the projects/tasks that we are obliged to do. I have never asked a programming question before, since if I wonder about something I can Google it or read it in my book. But now I've done all that, and I still have an error I can't get by.

I want to read in a value (integer or double) from a Console application. Then I want to make this value a Double, so that I can use it in my calculations. It's a simple task about calculating resistance in a electric circuit.

I have this code:
C#:
intMotstandsVerdi = Convert.ToInt32(Console.Read());
dblMotstandsVerdi = Convert.ToDouble(intMotstandsVerdi);
It gives me this error:
CS0266 Cannot implicitly convert type 'double' to 'int'. An explicit conversion exists (are you missing a cast?)

(It's the same error if I use Console.ReadLine() instead.

Then I try this code, and also get an error:
C#:
dblMotstandsVerdi = Double.Parse(Console.ReadLine());
It gives me he same error:
CS0266 Cannot implicitly convert type 'double' to 'int'. An explicit conversion exists (are you missing a cast?)
I have googled and googled, but all the answers are that you have to Parse the input or convert the integer to Double. That's why I'm asking this question.

I hope anyone of you could please help me with this error. All help would be very much appriceated! Thanks!
 
Last edited by a moderator:

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
4,646
Location
Sydney, Australia
Programming Experience
10+
That suggests that you have declared the dblMotstandsVerdi variable as type int rather than type double.

As far as what code to use, go with the second option, i.e. convert directly from string to double. I assume that you only converted to int in between because this error message made you think that you needed to for some reason. You don't. You just have to have a variable of type double to assign the double value to.
 

NoUserHere

Well-known member
Joined
Sep 5, 2018
Messages
2,138
Programming Experience
10+
I want to read in a value (integer or double) from a Console application.
When taking the value from a user in a console application, the right thing to do is to check if the user is first giving you a numeric value. Second, check the type of number and then parse that value. By using Double.Parse You are assuming that the value is a double. That will throw an exception if its not. A safer way to do it is to use Double.TryParse instead. This will fail silently if it fails and won't throw any exception. Double.TryParse Method (System)
I would only recommend Double.Parse if you are 100% sure of the value been given.

Try this : Question - I need help with a code
 
Top Bottom