Reputation: 23
I am working on a calculator and whenever my calculator adds up a number it just joins them together.
An example of this is 3+5 it gives me 35. I converted the numbers into doubles and it still gives me 35. Here is my code:
Console.Write("Enter a Number: ");
double num1 = Convert.ToDouble(Console.ReadLine());
Console.Write("Enter a Operator: ");
string op = Console.ReadLine();
Console.Write("Enter a Number: ");
double num2 = Convert.ToDouble(Console.ReadLine());
if (op == "+")
{
Console.WriteLine(num1 + " plus " + num2 + " is " + num1 + num2);
}
Console.ReadLine();
Thank you for viewing my post have a nice day.
Upvotes: 2
Views: 91
Reputation:
Because there are strings involved in your expression using +
, and because of the order in which those string arguments appear in that expression, the C# compiler treats the +
operators as string concatenation.
In more detail: The +
are interpreted as string concatenation because the expression is evaluated from left to right. The first evaluated term is num1 + " plus "
. Since there is a string involved, the first +
is treated as operator for string concatenation. With the first term evaluated to a string, the second and following +
each with their respective second argument are then also treated as string concatenation, because the left-hand side of each (partial) term is always being evaluated to a string.
A "cheap" way to fix the code is to use parantheses to help the compiler to distinguish between the arithmetic operator and the string concatenation operator:
Console.WriteLine(num1 + " plus " + num2 + " is " + ( num1 + num2 ) );
A safer and more readable way to write the output can be achieved by using string interpolation (note the $
symbol in front of the string), which conveniently gets rid of the +
operator confusion:
Console.WriteLine($"{num1} plus {num2} is {num1 + num2}");
(Thanks to @AlexeiLevenkov for pointing out the latter)
Upvotes: 3
Reputation: 81493
The other answers are sufficient and explain the problem, though you might like to use String Interpolation, and double.TryParse
to validate the input and give feedback on failure
The $ special character identifies a string literal as an interpolated string. An interpolated string is a string literal that might contain interpolated expressions. When an interpolated string is resolved to a result string, items with interpolated expressions are replaced by the string representations of the expression results. This feature is available in C# 6 and later versions of the language.
Converts the string representation of a number to its double-precision floating-point number equivalent. A return value indicates whether the conversion succeeded or failed.
Example
// valid operators
var ops = new[] { "+", "-", "/", "*" };
double num1;
Console.Write("Enter a Number: ");
while (!double.TryParse(Console.ReadLine(), out num1))
Console.Write("Invalid number, try again");
string op;
Console.Write($"Enter operator (\"+\", \"-\", \"/\", \"*\"): ");
while (!ops.Contains(op = Console.ReadLine()))
Console.Write("Invalid operator, try again");
double num2;
Console.Write("Enter a Number: ");
while (!double.TryParse(Console.ReadLine(), out num2))
Console.Write("Invalid number, try again");
// local function to do stuff
double ApplyOperator()
{
switch (op)
{
case "+": return num1 + num2;
case "-": return num1 - num2;
case "/": return num1 / num2;
case "*": return num1 * num2;
default: throw new InvalidOperationException();
}
}
// an interpolated string
Console.WriteLine($"{num1} {op} {num2} = {ApplyOperator()}");
Console.ReadLine();
Output
Enter a Number: 1
Enter operator ("+", "-", "/", "*"): +
Enter a Number: 2
1 + 2 = 3
Upvotes: 0