C#

Moderators: None (Apply to moderate this forum)
Number of threads: 2720
Number of posts: 5746

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Forever asking for help Posted by Opticknerve on 20 May 2009 at 10:12 PM
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.

/*
 * 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.
Report
Re: Forever asking for help Posted by Psightoplazm on 21 May 2009 at 1:29 PM
sticking strictly to your example you would want to add a new method to your class:
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;
}


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

now here is the easiest method to understand:
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;
    }
}


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:
Console.WriteLine("Please enter your equation:");
var equation = Console.ReadLine();
var data = Regex.Match(equation, "(?<val1>\\d+)\\s*(?<sign>[+\\-*/])\\s*(?<val2>\\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);



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 this page for more info
></\/~Psightoplasm`~
Report
Re: Forever asking for help Posted by Opticknerve on 21 May 2009 at 10:49 PM
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.
Report
Re: Forever asking for help Posted by Opticknerve on 21 May 2009 at 10:50 PM
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.



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.