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; }


LinkBack URL
About LinkBacks






Reply With Quote







Bookmarks
Algorithms and Data Structures
Java tutorials
Algorithms Forum