Go Back   CodeCall Programming Forum > Software Development > Tutorials > C Tutorials
Register Blogs Search Today's Posts Mark Forums Read

C Tutorials All C Tutorials and Code

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-10-2009, 11:44 AM
nicckk's Avatar
Guru
 
Join Date: Oct 2008
Location: San Diego
Posts: 569
nicckk has a spectacular aura aboutnicckk has a spectacular aura about
Send a message via Yahoo to nicckk
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 11:56 AM.. Reason: end a code tag
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 03-10-2009, 11:58 AM
Jordan's Avatar
Administrator
 
Join Date: Nov 2005
Location: Hendersonville, NC
Posts: 24,556
Jordan is a name known to allJordan is a name known to allJordan is a name known to allJordan is a name known to allJordan is a name known to allJordan is a name known to all
Send a message via ICQ to Jordan Send a message via AIM to Jordan Send a message via MSN to Jordan Send a message via Yahoo to Jordan
Re: How to program a calcuator in C++

Very nice tutorial! +rep
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 03-10-2009, 11:58 AM
WingedPanther's Avatar
Super Moderator
 
Join Date: Jul 2006
Age: 36
Posts: 11,435
WingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud of
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 03-10-2009, 12:38 PM
nicckk's Avatar
Guru
 
Join Date: Oct 2008
Location: San Diego
Posts: 569
nicckk has a spectacular aura aboutnicckk has a spectacular aura about
Send a message via Yahoo to nicckk
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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 03-10-2009, 12:51 PM
WingedPanther's Avatar
Super Moderator
 
Join Date: Jul 2006
Age: 36
Posts: 11,435
WingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud of
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 03-10-2009, 12:58 PM
nicckk's Avatar
Guru
 
Join Date: Oct 2008
Location: San Diego
Posts: 569
nicckk has a spectacular aura aboutnicckk has a spectacular aura about
Send a message via Yahoo to nicckk
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:
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 03-10-2009, 01:37 PM
WingedPanther's Avatar
Super Moderator
 
Join Date: Jul 2006
Age: 36
Posts: 11,435
WingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud of
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
[HELP] C++ program that will ask the user to enter not greater than to 15 numbers Kyram143 C and C++ 8 12-16-2009 02:53 PM
Tutorial: Starting C# with C# 2008 Express Edition Jordan CSharp Tutorials 20 07-27-2009 05:45 AM
Debugging a C++/C File with GDB Debugger awesome001 C and C++ 2 01-01-2009 08:07 PM
Help with Square root and calculator program!!! 123456789asdf C and C++ 10 12-02-2007 05:35 PM


All times are GMT -5. The time now is 11:18 AM.


vBulletin v3.8.0 ©2010, Jelsoft Enterprises Ltd.


no new posts

LinkBacks Enabled by vBSEO 3.1.0