+ Reply to Thread
Results 1 to 7 of 7

Thread: How to program a calcuator in C++

  1. #1
    Join Date
    Oct 2008
    Location
    San Diego
    Posts
    629
    Rep Power
    19

    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 08:56 AM. Reason: end a code tag

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Jordan Guest

    Re: How to program a calcuator in C++

    Very nice tutorial! +rep

  4. #3
    Join Date
    Jul 2006
    Posts
    16,486
    Blog Entries
    75
    Rep Power
    143

    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?
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  5. #4
    Join Date
    Oct 2008
    Location
    San Diego
    Posts
    629
    Rep Power
    19

    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.

  6. #5
    Join Date
    Jul 2006
    Posts
    16,486
    Blog Entries
    75
    Rep Power
    143

    Re: How to program a calcuator in C++

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

  7. #6
    Join Date
    Oct 2008
    Location
    San Diego
    Posts
    629
    Rep Power
    19

    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:

  8. #7
    Join Date
    Jul 2006
    Posts
    16,486
    Blog Entries
    75
    Rep Power
    143

    Re: How to program a calcuator in C++

    Interesting... GCC 4.1 gave me "nan" for the output of the division.
    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. Program for search terms within a program
    By 817moose in forum General Programming
    Replies: 4
    Last Post: 11-03-2010, 01:07 PM
  2. Why do you program/Why are you learning to program?
    By taylerhughes in forum The Lounge
    Replies: 34
    Last Post: 08-11-2010, 12:26 AM
  3. Replies: 9
    Last Post: 03-14-2010, 08:44 PM
  4. Execute a program from within another program
    By gabrielle_l in forum Java Help
    Replies: 4
    Last Post: 02-17-2010, 02:47 PM
  5. Replies: 8
    Last Post: 05-11-2009, 01:41 AM

Tags for this Thread

Bookmarks

Posting Permissions

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