+ Reply to Thread
Results 1 to 7 of 7

Thread: How to program a calcuator in C++

  1. #1
    Guru nicckk has a spectacular aura about nicckk has a spectacular aura about nicckk's Avatar
    Join Date
    Oct 2008
    Location
    San Diego
    Posts
    571

    How to program a calcuator in C++

    Have you ever been tired of using a physical calculator, or program your own? This tutorial will help you. (This tut will not teach you how to start programming in C++.)

    First setup the preprocesser, the main function,

    Code:
    #include <iostream> 
    using namespace std;
    int main() 
    {
    Then define two variables that the user can select. I used a double instead of an int. so the input or output could be a decimal.

    Code:
        // One and two are the numbers the user selects
         double one, two;
    Then we will display two lines of text, along with a "cin" so we can get the users input

    Code:
        cout << "Please input the first number:";
        cin >> one; // Cin takes the users first number, and associates it to one
        cout << "Please input the second number:";
        cin >> two; // Here cin takes the second number and assigns it to two
    Then the math comes into play. In C++ a + sign is used to indicate addition, a - sign for subtraction, a * for multiplication, and a / for division.

    Code:
        cout << "\nThe answers are:\nAddition:" << one + two; // All the math happens here
        cout << "\nSubtraction:" << one - two;
        cout << "\nMultiplication:" << one * two;
        cout << "\nDivision:" << one / two;
        cout << "\nThank you for using myCalc."; 
        cout << "\n";
        main(); // This starts the main function again, so you can do another calculation
    And finally end the main function

    Code:
        system("PAUSE"); 
        return 0;
    }
    So in total our code looks like this
    Code:
    #include <iostream> 
    using namespace std;
    int main() 
    {
        // One and two are the numbers the user selects
         double one, two;
        
        cout << "Please input the first number:";
        cin >> one; // Cin takes the users first number, and assosicates it to one
        cout << "Please input the second number:";
        cin >> two; // Here cin takes the second number and assisgns it to two
        cout << "\nThe answers are:\nAddition:" << one + two; // All the math happens here
        cout << "\nSubtraction:" << one - two;
        cout << "\nMultiplication:" << one * two;
        cout << "\nDivision:" << one / two;
        cout << "\nThank you for using myCalc."; 
        cout << "\n";
        main(); // This starts the main function again, so you can do another calculation
        
        system("PAUSE"); 
        return 0;
    }
    Thanks for reading.
    Last edited by WingedPanther; 03-10-2009 at 10:56 AM. Reason: end a code tag

  2. #2
    Administrator Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan's Avatar
    Join Date
    Nov 2005
    Location
    Hendersonville, NC
    Posts
    24,556
    Blog Entries
    97

    Re: How to program a calcuator in C++

    Very nice tutorial! +rep

  3. #3
    Super Moderator WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther's Avatar
    Join Date
    Jul 2006
    Age
    36
    Posts
    11,660
    Blog Entries
    57

    Re: How to program a calcuator in C++

    It's a nice intro to basic input/output and the operations.

    A few observations:
    1) you can't pick your operation
    2) your program will crash if two == 0.
    3) wouldn't a loop be more useful than a recursive call?
    CodeCall Blog | CodeCall Wiki | Shareware
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  4. #4
    Guru nicckk has a spectacular aura about nicckk has a spectacular aura about nicckk's Avatar
    Join Date
    Oct 2008
    Location
    San Diego
    Posts
    571

    Re: How to program a calcuator in C++

    Quote Originally Posted by WingedPanther
    It's a nice intro to basic input/output and the operations.

    A few observations:
    1) you can't pick your operation
    2) your program will crash if two == 0.
    3) wouldn't a loop be more useful than a recursive call?
    1. I was trying to make the program as simple as possible. If I went to revise it though, I would like to make it so you can select an operation

    2. How would this come to be? I ran the program and put 0 in for one and two, and nothing happened to the program.

    3. Sure, but once again, I was trying to make it simple. Thank's for your feedback WP, and Jordan.

  5. #5
    Super Moderator WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther's Avatar
    Join Date
    Jul 2006
    Age
    36
    Posts
    11,660
    Blog Entries
    57

    Re: How to program a calcuator in C++

    0/0 is generally not a good idea.
    CodeCall Blog | CodeCall Wiki | Shareware
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  6. #6
    Guru nicckk has a spectacular aura about nicckk has a spectacular aura about nicckk's Avatar
    Join Date
    Oct 2008
    Location
    San Diego
    Posts
    571

    Re: How to program a calcuator in C++

    Quote Originally Posted by WingedPanther View Post
    0/0 is generally not a good idea.

    Please input the first number:0
    Please input the second number:0

    The answers are:
    Addition:0
    Subtraction:0
    Multiplication:0
    Division:-1.#IND
    Thank you for using myCalc.
    Please input the first number:

  7. #7
    Super Moderator WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther's Avatar
    Join Date
    Jul 2006
    Age
    36
    Posts
    11,660
    Blog Entries
    57

    Re: How to program a calcuator in C++

    Interesting... GCC 4.1 gave me "nan" for the output of the division.
    CodeCall Blog | CodeCall Wiki | Shareware
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Similar Threads

  1. Replies: 8
    Last Post: 12-16-2009, 01:53 PM
  2. Tutorial: Starting C# with C# 2008 Express Edition
    By Jordan in forum CSharp Tutorials
    Replies: 20
    Last Post: 07-27-2009, 04:45 AM
  3. Debugging a C++/C File with GDB Debugger
    By awesome001 in forum C and C++
    Replies: 2
    Last Post: 01-01-2009, 07:07 PM
  4. Help with Square root and calculator program!!!
    By 123456789asdf in forum C and C++
    Replies: 10
    Last Post: 12-02-2007, 04:35 PM

Bookmarks

Bookmarks

     
        Algorithms and Data Structures

        Java tutorials

        Algorithms Forum

Posting Permissions

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