Reputation:
So this is my first question on StackExchange. I'm a Programmer who uses many Programming Languages, so I'm a bit ashamed that I'm missing what could be happening here. I'm using Mono on Xubuntu 18.04, and whenever I try and compile the Code below, it throws back a bunch of errors about Variables I declared, and the else keyword. I followed along with a C# tutorial on YouTube, and it's driving me crazy trying to figure out where the error(s) are. Here's the code, which I edited a bit to personal taste:
using System;
class A_Better_Way
{
static void Main(string[] args)
{
Console.Write("Enter a number:");
double num_one = Convert.ToDouble(Console.ReadLine()); //Doubles allow the use of decimals
Console.Write("Enter \"+\", \"-\", \"/\" or \"*\":");
string operator = Console.ReadLine();
Console.Write("Enter another one:");
double num_two = Convert.ToDouble(Console.ReadLine());
if(operator == "+" ) //If Statements: Easy as magic. Easy as Jiff.
{
Console.WriteLine(num_one + num_two);
}
else if(operator == "-")
{
Console.WriteLine(num_one - num_two);
}
else if(operator == "/")
{
Console.WriteLine(num_one / num_two);
}
else if(operator == "*")
{
Console.WriteLine(num_one * num_two);
}
else
{
Console.WriteLine("You fail at life.");
}
Console.ReadLine();
}
}
Any help would be most appreciated. And, I'm glad to be here.
Upvotes: 0
Views: 976
Reputation: 2299
Welcome to SO!
operator
is a keyword in C#, so you cannot use it as variable name.
https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/operator
For a future, please specify error messages you got. It will help others to understand your problem and give an accurate answer.
Upvotes: 2