Forever asking for help

[code][size=5]How do i get the program to ask the user to input the numbers, and then ask the user to input the operator.

Instead of just having the user inputting only the operators.
[/size]
/*
* Created by Opticknerve
* User: Administrator
* Date: 5/20/2009
* Time: 8:59 AM
*
*/

using System;
class Maths
{
public static void Main()
{
Maths m = new Maths();
m.ShowDemo();
}
void ShowDemo()
{

int Addition = 0;
int Subtraction = 0;
int Multiplication = 0;
int Devision = 0;

int num1 = 60;
int num2 = 50;

Addition = num1 + num2;
Subtraction = num1 - num2;
Multiplication = num1 * num2;
Devision = num1/num2;

Console.WriteLine("PLease choose any of the following operators +,-,*,/");
string Sign = Console.ReadLine();
switch (Sign)
{
case "+":
Console.WriteLine("You have chosen the + operator");
Console.WriteLine("Number 1 + Number 2 = :" + Addition);
break;
case "-":
Console.WriteLine("You have chosen the - operator");
Console.WriteLine("Number 1 - Number 2 = :" + (Subtraction));
break;
case "*":
Console.WriteLine("You have chosen the * operator");
Console.WriteLine("Number 1 * Number 2 = :" + (Multiplication));
break;
case "/":
Console.WriteLine("You have chosen the / operator");
Console.WriteLine("Number 1 / Number 2 = :" + (Devision));
break;
default:
Console.WriteLine("Please Choose an operator");
break;
}
}
}

Thanx Psightoplazm for your help.
[/code]

Comments

  • sticking strictly to your example you would want to add a new method to your class:
    [code]
    private double DoMath(int val1, int val2, string oper)
    {
    switch (oper)
    {
    case "+":
    return (val1 + val2);
    case "-":
    return (val1 - val2);
    case "*":
    return (val1 * val2);
    case "/":
    return (val1 / val2);
    }
    return 0;
    }
    [/code]

    Then in your main code you will want to ask the user for the additional input:

    now here is the easiest method to understand:
    [code]
    using System;
    class Maths
    {
    public static void Main()
    {
    Maths m = new Maths();
    m.ShowDemo();
    }
    void ShowDemo()
    {
    Console.Write("Please enter the first value:");
    var num1 = int.Parse(Console.ReadLine());
    Console.Write("Please enter the second value:");
    var num2 = int.Parse(Console.ReadLine());
    Console.Write("Please enter the desired function (+,-,*,/)");
    var oper= Console.ReadLine();
    var result = DoMath(num1, num2, oper);

    Console.WriteLine("The result to your equation is: " + result);
    }

    private double DoMath(int val1, int val2, string oper)
    {
    switch (oper)
    {
    case "+":
    return (val1 + val2);
    case "-":
    return (val1 - val2);
    case "*":
    return (val1 * val2);
    case "/":
    return (val1 / val2);
    }
    return 0;
    }
    }
    [/code]

    However if you wanted to make this a little easier for the user you might want to use a regular expression to parse a plain text expression:
    [code]
    Console.WriteLine("Please enter your equation:");
    var equation = Console.ReadLine();
    var data = Regex.Match(equation, "(?\d+)\s*(?[+\-*/])\s*(?\d+)");
    var val1 = int.Parse(data.Groups["val1"].Value);
    var val2 = int.Parse(data.Groups["val2"].Value);
    var operator = data.Groups["sign"].Value;
    var result = DoMath(val1, val2, operator);
    Console.WriteLine("Your answer is " + result);

    [/code]

    To explain some of the stuff in there -
    int.Parse - is a way of converting a 'string' to an 'int'
    A regular expression matches a string to a pattern and allows you to pull information from it - please see [link=http://www.codeproject.com/KB/dotnet/regextutorial.aspx]this page[/link] for more info
    ><//~Psightoplasm`~
  • Hey thanx psightoplazm. The regular expressions are very complex at he moment but i think with time and dedication, i will get a hang of them thanx alot for your help.
  • Hey thanx psightoplazm. The regular expressions are very complex at he moment but i think with time and dedication, i will get a hang of them thanx alot for your help.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories