Hello everyone,
I'm trying to make a simple calculator that doesn't care about priority and only do operation linearly.
Example: 4 + 5 * 2 = 18
Here's my code so far:
However when I try it with a cmd, it crashes... Any ideas ?
Thanks in advance
I'm trying to make a simple calculator that doesn't care about priority and only do operation linearly.
Example: 4 + 5 * 2 = 18
Here's my code so far:
static void Main(string[] args)
{
int i = 0;
float res;
int p;
int num2 = 0;
int num1 = 0;
if (int.TryParse(args[1], out p))
{res = p;} // sets the res as the first number of the operation
while (i < args.Length) // Doing the whole length of what we enterd
{
switch (args[i])
{
case "+" : // If it detects a +
if (int.TryParse(args[i+1], out p)) // Take the next argument
{
num2 = Int32.Parse(args[i+1]); // Make it an int
res += num2; // Add it to res (which is the 1st argument and the total of before)
}
break;
case "-":
if (int.TryParse(args[i + 1], out p))
{
num2 = Int32.Parse(args[i + 1]);
res -= num2;
}
break;
case "*":
if (int.TryParse(args[i + 1], out p))
{
num2 = Int32.Parse(args[i + 1]);
res *= num2;
}
break;
} i++;
}
Console.WriteLine(res.ToString()); // Says unassigned res
Console.ReadLine();
}
However when I try it with a cmd, it crashes... Any ideas ?
Thanks in advance