简体   繁体   中英

Error CS0029 Cannot implicitly convert type 'string' to 'double'

I am creating an C# calculator console using visual studio and code and when I am typing and debugging my application I got this error "Error CS0029 Cannot implicitly convert type 'string' to 'double'" . I think my code was not compatible. Can somebody help me?

Console.WriteLine("\t\t\tCalculator in C#\r");
Console.WriteLine("\t\t\t-----------------\r");
 
Console.WriteLine("\t\tEnter First Number\r"); 
double num1 = Double.Parse(Console.ReadLine());

Console.WriteLine("\t\tSelect an Operator: ( +, -, *, /, ^)\r");

 
double opp = Console.ReadLine(); ---- WITH THIS LINE??**
 
Console.WriteLine("\t\tEnter second Number\r"); 
double num2 =  Double.Parse(Console.ReadLine());

In the particular line, you are expecting an operator. This should be represented by a char or string , rather than double.

Console.WriteLine("\t\tSelect an Operator: ( +, -, *, /, ^)\r");
string opp = Console.ReadLine(); // Change here

The operators +-*/^ are not number and cannot be represented by double.You can add additional checks to ensure, the User has entered a valid operator

It's because Console.ReadLine() is returning string. But you are trying to put that string in your double variable. If you are expecting user to type in the operator (+, -, ...), then make your opp variable of type string . If is user supposed to send a number for operator(eg. 1 for '+', 2 for '-', 3 for '*'), then change the line to

double opp = Douoble.Parse(Console.ReadLine());

In both cases you should let user know what type of input are you expecting.

If anything is not clear, let me know for sure

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM