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.
|
Suthish NairPosted Jan 31, 2011, 1:02 AM
Check Our recommended articles section on right side of your browser.
Having examples of calculator.
FrankPosted Jan 30, 2011, 6:55 PM
Roy SPosted Jan 30, 2011, 6:38 PM
'+' means the char + and "+" means a string containing a +.
FrankPosted Jan 30, 2011, 6:22 PM
on the if statement
if (userinput == '+')
Roy SPosted Jan 30, 2011, 5:19 PM
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();
}
FrankPosted Jan 30, 2011, 4:57 PM
Suthish NairPosted Jan 30, 2011, 1:37 PM
So other members can easily find the answers.
Roy SPosted Jan 30, 2011, 7:14 AM
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 NairPosted Jan 30, 2011, 4:39 AM