{Tutorial} How to make a simple calculator

Botch Sep 1, 2013

  1. Botch

    Botch Newbie
    25/47

    Joined:
    Feb 1, 2013
    Messages:
    34
    Likes Received:
    28
    Trophy Points:
    0
    Gender:
    Male
    Location:
    New Jersey
    Console:
    Xbox
    Hey guys, I haven't been on XPG for months, so I decided to bring life back to this section with a very simple tutorial for a very simple calculator in C#.
    This was compiled with Microsoft Visual Studios 2012 Edition, though you can use any version which has C#.

    MVS 2012 (or 2010 C# edition): http://www.microsoft...#d-2012-express

    Now, let's get started, shall we?

    So first off, you want to download Visual Studios from the link above. Any version will work, but if you download the 2012 express edition it will be a lot easier for you to follow.

    Once you have downloaded the compiler, open it, and you should see a screen like this:
    http://puu.sh/4fgoZ.png

    As you can see, MVS has options for creating new projects, opening saved ones, and it has your five most recent projects on the startup. Select the option entitled 'New Project' and this screen will appear:
    http://puu.sh/4fgtl.png

    This is the interface for creating projects in multiple languages. Choose C#, and choose 'Console Application' and name it whatever you wish (as you can see mine is called 'Botch is a pimp').

    The program's source will now load as soon as you click 'OK'.
    If you have done this correctly, you will see something along the lines of this:
    http://puu.sh/4fgAf.png

    Let me dissect this for you guys:

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    using System.Threading.Tasks;//everything above and including this is just including other namespaces



    namespace Botch_is_a_pimp//name of project, used to signify the block of code

    {

    class Program//anything inside of this can be accessed by using Program.Whatever

    {

    static void Main(string[] args)//inside these curly braces is where your code will be

    {

    }

    }

    }



    Now we can begin the actual coding.

    Open up the two curly braces by entering a space between them.
    In C# (and most other languages) the curly braces are methods, or blocks of code that can be executed.
    Every line inside these must end with a semicolon.

    Now, we REALLY begin.

    Above the static void Main(string[] args) type in static double total;
    This will be your total number at the end.
    While many use integers for numbers, I use doubles because they have very large amounts they can hold.

    Now, inside of the static void you want to enter:

    double num1;

    double num2;

    char op = '+';

    bool repeat = true;
    Basically, num1 and num2 are going to be the numbers you enter to add, subtract, multiply, or divide.
    Op (shortened version of operator) is going to be used to add, subtract, multiply, or divide.
    And the bool reapeat = true, we will come to in a bit.

    Your code should now look like this:
    http://puu.sh/4fgZE.png

    As you can see, we have warnings because the variables we assigned are created, but not used, which wastes memory.
    However, you don't need to know about memory, or care about warnings.
    Only care about errors, because then your program cannot build.

    Now, under the bool repeat = true, add in this:

    while(true)//meaning that this will only happen when repeat = true

    {

    Console.WriteLine("Calculator by <insert name>");

    Console.WriteLine("Please enter your first number: ");

    num1 = Convert.ToDouble(Console.ReadLine());

    //more stuff in a bit

    }
    Basically what this is doing is printing the words Calculator by 'Your name' on the first line, then on the second line it says Please enter your first number:
    num1 = Convert.ToDouble(Console.ReadLine()); makes it when you enter a number, it carries that value to num1;
    So if you entered 420, the once blank num1 is now 420.
    Now, do the same for num2.

    Your coding should now look like this:
    http://puu.sh/4fhja.png

    Now enter in the following coding, which I will dissect apart from the picture:
    http://puu.sh/4fhrM.png

    Now what you are doing here is converting the character 'op' to what is read by the console.
    A switch statement creates different outcomes for different results, which is when you use the different math operators, they do different things.

    Now you can see that we are applying the 'total' double, which is equal to num1 and num2 with the operator.

    Add in this finishing touch, and you're done!
    http://puu.sh/4fhyy.png

    Now try it out, and see if there are any bugs. The compiler will tell you if there are syntax errors, but generally there are bugs on the first build not caught by the compiler.

    Hope you guys learned something, and if you have any problems, just ask me. [​IMG]

    EDIT: PIcture of working application (thanks CIA):
    [​IMG]
     
  2. expokiki

    expokiki Banned BANNED
    45/47

    Joined:
    Oct 17, 2011
    Messages:
    680
    Likes Received:
    172
    Trophy Points:
    45
    Gender:
    Male
    Location:
    connecticut
    Console:
    Xbox
    Thanks nice little tu I have to try this
     
  3. Timmeh

    Timmeh Trve Kvlt
    25/47

    Joined:
    Jan 18, 2013
    Messages:
    468
    Likes Received:
    155
    Trophy Points:
    25
    Gender:
    Male
    Occupation:
    Drummer
    Location:
    Michigan
    Console:
    Xbox
    [​IMG]

    [​IMG]

    Very nice tutorial here Botch! :boobs:
     

Share This Page

Close