how can i fix that code
i need to get 2 number and operation then do num1 then the operation and then num 2
lets say i get num1 = 2, num 2 = 4 and operation = + so Console need to print 2+4
10 Replies
thats what i got so far
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _1.cs
{
class Program
{
static void Main(string[] args)
{
Console.Write("Enter the first number ");
int num1 = int.Parse(Console.ReadLine());
Console.Write("Enter the second number");
int num2 = int.Parse(Console.ReadLine());
Console.Write("Enter the operation (+, -, *, /)");
int operation = int.Parse(Console.ReadLine());
if (operation = / )
{
Console.WriteLine(num1 / num2);
}
if (operation = * )
{
Console.WriteLine(num1 * num2);
}
if (operation = - )
{
Console.WriteLine(num1 - num2);
}
if (operation = + )
{
Console.WriteLine(num1 + num2);
}
}
}
}
how can i fix it
only the operation
im a starter so like
Do you mean that you should literally print to the console:
"2+4"
And not do the calculation and print the result "6"?
No
I get 2 random numbers
And a random operation
yeah I get that, I was just using the ones from your example
so do you actually have errors, or it's just not printing what you want it to print?
@SinFluxx
ok, few things, if you're comparing things (i.e. in your if statements) then you need to use double =:
if (x == y)
A single = means you're assigning a value
Also your operator should be a string, so you need to make sure you're comparing it to a string:
if (operator == "/")
i.e. it needs double quotes around the operator
You have this where you're reading your operator from user input:
int operation = int.Parse(Console.ReadLine())
I don't think you want it to be an int?See my first point about using double = in if statements
ye ye mb
and the sec problome?
you've still got
int.Parse
in there