Very nice tutorial! +rep
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,
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:#include <iostream> using namespace std; int main() {
Then we will display two lines of text, along with a "cin" so we can get the users inputCode:// One and two are the numbers the user selects double one, 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 << "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
And finally end the main functionCode: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
So in total our code looks like thisCode:system("PAUSE"); return 0; }
Thanks for reading.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; }
Last edited by WingedPanther; 03-10-2009 at 07:56 AM. Reason: end a code tag
Very nice tutorial! +rep
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
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog
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 operationOriginally Posted by WingedPanther
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.
0/0 is generally not a good idea.
CodeCall Blog | CodeCall Wiki
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog
Interesting... GCC 4.1 gave me "nan" for the output of the division.
CodeCall Blog | CodeCall Wiki
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog
There are currently 1 users browsing this thread. (0 members and 1 guests)