CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jan 2011
    Location
    Netherlands
    Posts
    45

    my simple console program is not working

    Hi,

    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?!
                }
            }
        }
    }

  2. #2
    Join Date
    Feb 2011
    Location
    United States
    Posts
    1,016

    Re: my simple console program is not working

    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.

    For example, this method:
    Code:
    void inputLhs()
    {
        double lhs;
        Console.WriteLine("Input first operand: ");
        lhs = Int32.Parse(Console.ReadLine());
    }
    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.

  3. #3
    Join Date
    Feb 2011
    Location
    United States
    Posts
    1,016

    Re: my simple console program is not working

    Actually, a better structure would be this:

    Code:
    class Program
    {
        static char getOper() { ... }
        static double getNumber(string prompt) { ... }
        static double calculate(char op, double lhs, double rhs) { ... }
        static void Main(string[])
        {
            calculate(getOper(), getNumber("Input LHS"), getNumber("Input RHS"));
        }
    }
    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 07: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.

  4. #4
    Join Date
    Jan 2011
    Location
    Netherlands
    Posts
    45

    Re: my simple console program is not working

    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:
    Code:
            static char getOperator()
            {
                char opr;
                bool status = true;
    
                while (status)
                {
                    Console.WriteLine("Input operator (+ , -, *, / and %): ");
                    opr = (char)Console.Read();
                    
                    if ((opr != '+') || (opr != '-') || (opr != '*') || (opr != '/') || (opr != '%'))
                    {
                        Console.WriteLine("Wrong operator has been entered, please input an operator again.");
                        Console.WriteLine("Input operator (+ , -, *, / and %): ");
                        opr = (char)Console.Read();
                    }
    
                    if ((opr == '+') || (opr == '-') || (opr == '*') || (opr == '/') || (opr == '%'))
                    {
                        status = false;
                    }
                }
    
                Console.WriteLine("Input operator (+ , -, *, / and %): ");
                opr = (char)Console.Read();
    
                return opr;
            }
    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'

    Thanks!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured