I am trying to use my C skills to make a simple console calculator, I have defined all the methods but I dont know where to call them, as it was not possible to call them in the main()...What I am doing wrong?
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace simpleCalc
{
class Program
{
private void welcomeMessage()
{
Console.WriteLine("Welcome to a simple calculator by www.ElectronicsPub.com");
Console.WriteLine("Press 'q' at anytime to quite");
}
void inputLhs()
{
double lhs;
Console.WriteLine("Input first operand: ");
lhs = Int32.Parse(Console.ReadLine());
}
void inputRhs()
{
double Rhs;
Console.WriteLine("Input Second operand: ");
Rhs = Int32.Parse(Console.ReadLine());
}
void inputOperator()
{
char opr;
Console.WriteLine("Input operator (+ , -, *, / and %): ");
opr = (char) Console.Read();
while ((opr != '+') || (opr != '-') || (opr != '*') || (opr != '/') || (opr != '%') || opr ==null)
{
Console.WriteLine("Wrong operator has been entered, please input an operator again.");
Console.WriteLine("Input operator (+ , -, *, / and %): ");
opr = (char)Console.Read();
}
}
double calculate (char opr, double lhs, double rhs)
{
switch (opr)
{
case '+':
return lhs + rhs;
break;
case '-':
return lhs - rhs;
break;
case '*':
return lhs * rhs;
break;
case '/':
return lhs / rhs;
break;
case '%':
return lhs % rhs;
break;
default:
return 0;
break;
}
}
void showResult(double result)
{
Console.WriteLine(result);
}
static void Main(string[] args)
{
char reset = 'n';
while (reset != 'q')
{
// Have to use above functions here?!
}
}
}
}
All of the methods declared in this class are instance methods (that is, tied to a specific INSTANCE of a class created with the new keyword) while Main(string[]) is declared static (as it has to be). You can not call instance methods from a static context (unless you are calling them on an object). You'll entire need to declare each of the methods static as well.
Also: any variables that you are planning to share between methods (that is, without passing them as arguments) will need to be declared as static outside of any methods.
Essentially does nothing (except bother the user). lhs is undefined outside of this method. If you want lhs to be available outside the scope of this single method do this:
Code:
class Program
{
static double lhs = 0;
static void inputLhs()
{
lhs = Int32.Parse(Console.ReadLine());
}
}
Hopefully that makes it somewhat more clear? Please ask for clarification if you are still confused!
Best Regards,
BioPhysEngr http://blog.biophysengr.net
--
All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.
Do you see how this avoids using global variables (like static double lhs = 0; in my last example)? Avoid globals whenever possible.
Last edited by BioPhysEngr; February 14th, 2011 at 06:28 PM.
Reason: left off a type in my code
Best Regards,
BioPhysEngr http://blog.biophysengr.net
--
All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.
Thanks a lot, I managed to make it work like the way you said.
I now have other problems, which I could not figure them out.
1 - How to make a dummy proof method which neatly gets operator from users (I mean they only can enter + - * / and %) I wrote one but it seems that it makes an endless loop:
2. How we can do something which while the program is running, user can quite the program by hitting 'q' or "Q" (if the caps is on) and reset the program (and by that I mean the lhs, rhs and operator) by pressing 'r' or 'R'
Bookmarks