This tutorial will show you how to create an easy calculator in c++!![]()
Its only an easy calculator and it can only calculate two numbers!
This is a Tutorial for beginners who want to learn more about c++.
But its also good to know a bit of c++ to understand the whole code
First we must take an input and some variables to the program :
#include <iostream>this includes the iostream library (which contain istream and ostream) that provides input and output functionality.Code:#include <iostream> //include the standard input and output library using namespace std; //includes the standard files double number1, number2, result;//these are the variables for the exercise char character;// this is the char variable for the operation character
using namespace std this includes th standard functionality of the c++ libraries. (You can also write std::cout or std::cin)
After that we can programming the main function
First we will ask the user for two numbers.
cout is an Object of class ostream (standard output stream)Code:cout <<"Enter an Exercise ? [+][-][*][/]" << endl; //text output for the user
with cout and the insertion operator << you bring the text ("Enter an Exercise ? [+][-][*][/]") to the console.
After that we must work with the user input for that we used the command cin and the extraction operator >>(the opposite of <<)
cin is an object of istream (standard input stream)Code:cin >> number1 >> character >> number2;
cin and the operator >> transfer the user input to the variables!
Then our program must decide which of the different operations (+-*/) the user had chose. It's possible to do that with an if construction but i think it's
better and more Comfortable to do that with a switch -case construction
The switch-case construction is like an long if-else construction.Code:switch (character) { case '+': result = number1 + number2; break; case '-': result = number1 - number2; break; case '/': result = number1 / number2; break; case '*': result = number1 * number2; break; default : cout <<"Error, wrong character !\n"; return 1; }
It's not easy and funny to read such a long if-else code.Code:If (character = '+') {result = number1 + number2; } else { if (character =='-') {result = number1 - number2; }}
The break after each case is very important it will leave the switch blockCode:switch ( expression ) case constant-expression : statement [default : statement]
Without the program would do all the cases after the right case.
Default :If there is no case match the default will used.
The character for division is / and for multiplication is *.
So, our little calculator is now ready to start.
finally here is the complete code :
Crash if division by 0 :Code:int main() { cout <<"Enter an Exercise ? [+][-][*][/]" << endl; //text output for the user cin >> number1 >> character >> number2;//this will declaring the variables //by the user switch (character) //this will decide which operation the user had chose { case '+': // if the character is + result = number1 + number2; //this will calculate the result break; case '-': // if the character is - result = number1 - number2; //this will calculate the result break; case '/': // if the character is / result = number1 / number2; //this will calculate the result break; case '*': // if the character is * result = number1 * number2; //this will calculate the result break; default : // if the character is wrong cout <<"Error, wrong character !\n"; //display the error message return 1; //close the program } cout << number1 << character << number2 //display the result for the user << "=" << result << endl;//display the result for the user system("PAUSE");//wait for the user }
If you test the code you will see that it will be cancel when you try to calculate X/0
this is an math error and if you dont want that the program cancel you can add an exception :
This will try to calculate the result. When you enter a division by 0 the throw will generate a ExceptionCode:case '/': // if the character is / try //this will try to calculate the exercise { result = number1 / number2; //this will calculate the result throw(1);// if there is any error it will start the exception break; } catch (int a)//this part will treat the error { cout <<"Division by 0"<<endl; //this will display the error system("PAUSE); //wait for the user return 1; //this close the program }
Which will treat the error and display an error message to the user.Code:try { // guarded code} except ( expression ) { // exception handler }
Then the return 1; will leave the code.
I hope you will enjoy this
Any questions, and positive feedback is welcome!
+rep is appreciated
Sorry for the bad English !
This Tutorial is produced by Bartimäus.
Last edited by Bartimäus; 01-23-2010 at 06:05 AM.
You might want to add exception handling for division by 0.
Could you explain more about what the code does?
Thanks good tutorial +rep
Nice tut +rep.![]()
Knowledge: Intermediate C#, Beginner AS3, HTML, CSS, Binary, Hex, Octal.
Science is only an educated theory, which we cannot disprove.
So you basically made the C++ version of my calculator.
Root Beer == System Administrator's Beer
Download the new operating system programming kit! (some assembly required)
Good tutorial. +rep
Yeah, I was just wondering if you saw mine or not. Also it reminds me I should probably make the more advanced calculator tutorial I had originally planned to do.
Good job on yours though, I definitely like the colors you added.
Root Beer == System Administrator's Beer
Download the new operating system programming kit! (some assembly required)
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks