-1

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());
Chris Catignani
  • 5,040
  • 16
  • 42
  • 49
CeRie
  • 1
  • 2

2 Answers2

0

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

Anu Viswan
  • 17,797
  • 2
  • 22
  • 51
0

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

Encyklopedie
  • 111
  • 1
  • 6