Add, Subtract, Divide, or Multiply. and I'm running into a problem, I'm trying to make it so if the user presses the + button it will automatically clear the screen(Which I haven't added to the code) and ask for the numbers.
The problem is it does not advance once the user chooses this.
Oh and also I'm a kind of new to C#. I'm not sure if the Convert.ToInt32's are necessary.
Code Code Code Code Code Code Code Code Code Code Code Code Code Code Code Code Code Code Code Code Code Code Code Code Code |

Suthish NairRank #92 · 22.8K reputationPosted Jan 29, 2011, 11:39 PM EST
Roy S194 reputationPosted Jan 30, 2011, 2:14 AM EST
The "Convert.ToInt32" can be changed to "Int32.Parse" or just "int.Parse".
And when displaying the result you can use "{0}" where the answer should be and add the calculaton as an extra parameter
This should work like you want it:
static void Main(string[] args)
{
int var1 = 0;
int var2 = 0;
int userInput = 0;
Console.WriteLine("Addition (+), Subtraction (-), Division (/), or Multiplicaton (*)");
userInput = Console.Read();
if (userInput == '+')
{
Console.WriteLine("Enter The First Number You Would Like To Add: ");
var1 = int.Parse(Console.ReadLine());
Console.WriteLine("Enter The Second Number You Would Like To Add: ");
var2 = int.Parse(Console.ReadLine());
Console.WriteLine("The Answer Is: {0}", var1 + var2);
}
Console.ReadKey();
}
Suthish NairRank #92 · 22.8K reputationPosted Jan 30, 2011, 8:37 AM EST
So other members can easily find the answers.
Frank16 reputationPosted Jan 30, 2011, 11:57 AM EST
Roy S194 reputationPosted Jan 30, 2011, 12:19 PM EST
This should work, I've changed the console.read to readline and made userInput a string:
static void Main(string[] args)
{
int var1 = 0;
int var2 = 0;
string userInput = "";
Console.WriteLine("Addition (+), Subtraction (-), Division (/), or Multiplication (*)");
userInput = Console.ReadLine();
if (userInput == "+")
{
Console.WriteLine("Enter The First Number You Would Like To Add: ");
var1 = int.Parse(Console.ReadLine());
Console.WriteLine("Enter The Second Number You Would Like To Add: ");
var2 = int.Parse(Console.ReadLine());
Console.WriteLine("The Answer Is: {0}", var1 + var2);
}
Console.ReadKey();
}
Frank16 reputationPosted Jan 30, 2011, 1:22 PM EST
on the if statement
if (userinput == '+')
Roy S194 reputationPosted Jan 30, 2011, 1:38 PM EST
'+' means the char + and "+" means a string containing a +.
Frank16 reputationPosted Jan 30, 2011, 1:55 PM EST
Suthish NairRank #92 · 22.8K reputationPosted Jan 30, 2011, 8:02 PM EST
Check Our recommended articles section on right side of your browser.
Having examples of calculator.