Mar 22, 2014 05:04 AM
(This post was last modified: Mar 22, 2014 06:18 AM by Niely.)
Hello
I’m still a beginner in C++ but with the knowledge I already have I succeeded to make a MINI-calculator in C++.
I thought it’d be cool to share it with you and maybe even post a mini-tutorial about it. :)
Like I said, I’m still a beginner so correct me if I’m wrong!
Things we will use
Cout<<”String”<<endl; To display/echo things on your screen.
Else/if statement To do something if user input is True/False
Cin >> Example; To make an user input.
Int var1, var2, var3; to say what variables we use.
Goto Label
EVERY code for the calculator should be between the brackets of main.
We use cout to display things on our screen, mostly strings (strings are everything between “ ” quotes), but also numbers or variable.
Examples:
Cout<<”Hello there”<<endl; String
Cout<<5<<endl; Number with VALUE
But we can also display variables with it:
This will display 123.
Cout<<”6”<<endl; Is NOT the same as cout<<6<<endl; because the second has a value, in this case 6. The first is just a string without value. :)
If you don’t use <<endl; but just: cout<<”Hello”;
This will display something but not jump to the next line.
Conclusion:
With cout we can display things
With cout we can display the value/input of a variable
With <<endl; we let our cursor jump to the next line.
We can display strings and values
Variables are actually little words/characters we give a value of meaning.
Every variable should be mentioned with int.
With int we say we use variables and how they call.
Example:
int variable1, variable2, var, total, myVariable;
You can set every variable on one line with int like we did above... But you can also separate them.
Example:
This will display:
7
4
With cin we can make an user input.
Example:
Cin >> myVariable;
Keep this in mind for the if/else statement! :)
The if/else statement for C++ looks a lot like the one in JavaScript
We already know we can give variables a value, let’s try it!
I said you to keep the cin option in mind, we can use it like this in the if/else statement:
“If user input (cin) is equal to eight, display True, or if x is equal to fifty-eight display Hey else display Error.”
You see? :)
You can have as much else if as you want!
Another useful piece of code is GoTo.
It looks like this:
Where myLabel is the label, a label should always end with “:”. With GoTo you can jump to that label and don’t have to re-write the whole piece of code each time, but you can just 'jump' to it. :)
Now the engine, the engine is very simple and based on the if/else statement.
The total engine to sum, looks like this:
Cin is the user input and has variable: sum1 for the first number; then we have another cin for the second number.
And the sum (Variable Sum) is equal to: Sum1 (number1) + sum2 (number2)
And then we cout sum (the total).
All other coutes are to make your output look nice, you can style it like you want.
And at the end of all, we go back to the beginning, so the user can calculate other things again without re-starting it.
If you know the engine for subtracting is like this:
And to multiply like this:
How will it looks to divide if you know it can be done with “/”?
Sum: +
Subtract: -
Multiply: *
Divide: /
Make sure to put all your variables in an int!
With this knowledge you can make your own calculator! :D
Sorry for my bad English/Broken explanation, just wanted to share! :D
Thanks for reading,
Niely
I’m still a beginner in C++ but with the knowledge I already have I succeeded to make a MINI-calculator in C++.
I thought it’d be cool to share it with you and maybe even post a mini-tutorial about it. :)
Like I said, I’m still a beginner so correct me if I’m wrong!
Things we will use
Cout<<”String”<<endl; To display/echo things on your screen.
Else/if statement To do something if user input is True/False
Cin >> Example; To make an user input.
Int var1, var2, var3; to say what variables we use.
Goto Label
Beginning
To begin with your calculator you should have:Code
#include <iostream>
Using namespace std;
Int main() {
}EVERY code for the calculator should be between the brackets of main.
Cout
We use cout to display things on our screen, mostly strings (strings are everything between “ ” quotes), but also numbers or variable.
Examples:
Cout<<”Hello there”<<endl; String
Cout<<5<<endl; Number with VALUE
But we can also display variables with it:
Code
int myVariable = 123
cout<<myVariable<<endl;Cout<<”6”<<endl; Is NOT the same as cout<<6<<endl; because the second has a value, in this case 6. The first is just a string without value. :)
If you don’t use <<endl; but just: cout<<”Hello”;
This will display something but not jump to the next line.
Conclusion:
With cout we can display things
With cout we can display the value/input of a variable
With <<endl; we let our cursor jump to the next line.
We can display strings and values
Variables
Variables are actually little words/characters we give a value of meaning.
Every variable should be mentioned with int.
With int we say we use variables and how they call.
Example:
int variable1, variable2, var, total, myVariable;
You can set every variable on one line with int like we did above... But you can also separate them.
Example:
Code
Int x = 7;
Cout<<x<<endl;
Int y = 4;
Cout<<y<<endl;This will display:
7
4
Cin
With cin we can make an user input.
Example:
Cin >> myVariable;
Keep this in mind for the if/else statement! :)
If/else statement
The if/else statement for C++ looks a lot like the one in JavaScript
Code
If (Statement) {
Do something
}
Else if (statement) {
Do something
}
Else {
Do something if none of above works/is true.
}We already know we can give variables a value, let’s try it!
Code
Int x = 8;
If (x==8) {
Cout<<”True”<<endl;
}
Else {
Cout<<”Error”<<endl;
}I said you to keep the cin option in mind, we can use it like this in the if/else statement:
Code
Int x;
Cin >> x;
If (x==8) {
Cout<<”True”<<endl;
}
Else if (x==58) {
Cout<<”Hey<<endl;
}
Else {
Cout<<”Error”<<endl;
}You see? :)
You can have as much else if as you want!
Labels and GoTo
Another useful piece of code is GoTo.
It looks like this:
Code
myLabel:
LOT OF CODE
GoTo myLabelWhere myLabel is the label, a label should always end with “:”. With GoTo you can jump to that label and don’t have to re-write the whole piece of code each time, but you can just 'jump' to it. :)
The Calculator Engine
Now the engine, the engine is very simple and based on the if/else statement.
The total engine to sum, looks like this:
Code
cout<<"Enter numbers to sum beneath eachother"<<endl;
cin >> sum1;
cout<<"+"<<endl;
cin >> sum2;
cout<<"="<<endl;
sum = sum1 + sum2;
cout<<" "<<endl;
cout<<sum<<endl;
cout<<" "<<endl;
cout<<"______"<<endl;
goto Start;Cin is the user input and has variable: sum1 for the first number; then we have another cin for the second number.
And the sum (Variable Sum) is equal to: Sum1 (number1) + sum2 (number2)
And then we cout sum (the total).
All other coutes are to make your output look nice, you can style it like you want.
And at the end of all, we go back to the beginning, so the user can calculate other things again without re-starting it.
If you know the engine for subtracting is like this:
Code
cout<<"Enter numbers to subtract beneath eachother"<<endl;
cin >> subs1;
cout<<"-"<<endl;
cin >> subs2;
cout<<"="<<endl;
substraction = subs1 - subs2;
cout<<" "<<endl;
cout<<substraction<<endl;
cout<<" "<<endl;
cout<<"______"<<endl;
goto Start;And to multiply like this:
Code
cout<<"Enter numbers to multiply beneath eachother"<<endl;
cin >> mul1;
cout<<"x"<<endl;
cin >> mul2;
cout<<"="<<endl;
mul = mul1 * mul2;
cout<<" "<<endl;
cout<<mul<<endl;
cout<<" "<<endl;
cout<<"______"<<endl;
goto Start;How will it looks to divide if you know it can be done with “/”?
Sum: +
Subtract: -
Multiply: *
Divide: /
Make sure to put all your variables in an int!
Choose what you want to do
But we also have to let the user decide what they want to do, we do that with the else/if statement and cin.Code
Cout<<”Press 1 to do this”<<endl;
Cout<<”Press 2 to do another thing”<<endl;
Cin >> Option;
If (option==1) {
Cout<<”This”<<endl;
}
Else if (option==2) {
Cout<<”Another thing”<<endl;
}
Else {
Cout<<”ERROR!!!”<<endl;
}With this knowledge you can make your own calculator! :D
Total Source Code:
Code
#include <iostream>
using namespace std;
int main() {
int option, sum1, sum2, sum, subs1, subs2, substraction, mul1, mul2, mul, div1, div2, div;
cout<<"~Calculator~"<<endl;
cout<<" "<<endl;
cout<<"Calculate Options:"<<endl;
cout<<"Press 1 to sum two numbers"<<endl;
cout<<"Press 2 to subtract two numbers"<<endl;
cout<<"Press 3 to multiply two numbers"<<endl;
cout<<"Press 4 to divide two numbers"<<endl;
cout<<" "<<endl;
cout<<"Other options:"<<endl;
cout<<"Enter 5 to view who made this"<<endl;
cout<<"Enter 6 to read about this calculator"<<endl;
Start:
cin >> option;
if (option==1) {
cout<<"Enter numbers to sum beneath eachother"<<endl;
cin >> sum1;
cout<<"+"<<endl;
cin >> sum2;
cout<<"="<<endl;
sum = sum1 + sum2;
cout<<" "<<endl;
cout<<sum<<endl;
cout<<" "<<endl;
cout<<"______"<<endl;
goto Start;
}
else if (option==2) {
cout<<"Enter numbers to subtract beneath eachother"<<endl;
cin >> subs1;
cout<<"-"<<endl;
cin >> subs2;
cout<<"="<<endl;
substraction = subs1 - subs2;
cout<<" "<<endl;
cout<<substraction<<endl;
cout<<" "<<endl;
cout<<"______"<<endl;
goto Start;
}
else if (option==3) {
cout<<"Enter numbers to multiply beneath eachother"<<endl;
cin >> mul1;
cout<<"x"<<endl;
cin >> mul2;
cout<<"="<<endl;
mul = mul1 * mul2;
cout<<" "<<endl;
cout<<mul<<endl;
cout<<" "<<endl;
cout<<"______"<<endl;
goto Start;
}
else if (option==4) {
cout<<"Enter numbers to divide beneath eachother"<<endl;
cin >> div1;
cout<<":"<<endl;
cin >> div2;
cout<<"="<<endl;
div = div1 / div2;
cout<<" "<<endl;
cout<<div<<endl;
cout<<" "<<endl;
cout<<"______"<<endl;
goto Start;
}
else if (option==5) {
cout<<"About me"<<endl;
cout<<"This calculator is made by Niely"<<endl;
cout<<"Webmaster of www.iCode4U.tk"<<endl;
cout<<" "<<endl;
cout<<"______"<<endl;
goto Start;
}
else if (option==6) {
cout<<"About Calculator"<<endl;
cout<<"This calculator is made for a tutorial for Hackforums.net"<<endl;
cout<<"This calculator can calculate: +,-,x,:. Good Luck! :D"<<endl;
cout<<" "<<endl;
cout<<"______"<<endl;
goto Start;
}
else {
cout<<"Option not found!"<<endl;
goto Start;
}
}Sorry for my bad English/Broken explanation, just wanted to share! :D
Thanks for reading,
Niely


