+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: Create a easy calculator

  1. #1
    Bartimäus's Avatar
    Bartimäus is offline Programming Expert
    Join Date
    Dec 2009
    Location
    Germany
    Posts
    490
    Rep Power
    11

    Smile Create an easy calculator

    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 :
    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 
    #include <iostream>this includes the iostream library (which contain istream and ostream) that provides input and output functionality.

    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.
    Code:
    cout <<"Enter an Exercise ? [+][-][*][/]" << endl; //text output for the user
    cout is an Object of class ostream (standard output stream)
    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 <<)
    Code:
    cin  >> number1 >> character >> number2;
    cin is an object of istream (standard input stream)
    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
    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;  
    }
    The switch-case construction is like an long if-else construction.
    Code:
    If (character = '+')
    {result = number1 + number2; }
    else { if (character =='-')
     {result = number1 - number2; 
    }}
    It's not easy and funny to read such a long if-else code.
    Code:
    switch ( expression )
          case constant-expression : statement
       [default  : statement]
    The break after each case is very important it will leave the switch block
    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 :
    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
    }
    Crash if division by 0 :
    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 :
    Code:
    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
    	}
    This will try to calculate the result. When you enter a division by 0 the throw will generate a Exception
    Code:
    try 
    {   // guarded code}
    except ( expression )
    {  // exception handler }
    Which will treat the error and display an error message to the user.
    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.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Join Date
    Jul 2006
    Posts
    16,478
    Blog Entries
    75
    Rep Power
    143

    Re: Create a easy calculator

    You might want to add exception handling for division by 0.
    Could you explain more about what the code does?
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  4. #3
    Hoffie is offline Newbie
    Join Date
    Jan 2010
    Posts
    3
    Rep Power
    0

    Re: Create a easy calculator

    Thanks good tutorial +rep

  5. #4
    Bartimäus's Avatar
    Bartimäus is offline Programming Expert
    Join Date
    Dec 2009
    Location
    Germany
    Posts
    490
    Rep Power
    11

    Re: Create a easy calculator

    Quote Originally Posted by WingedPanther View Post
    You might want to add exception handling for division by 0.
    Could you explain more about what the code does?
    Thanks for the good idea with division by 0! I would add a try exception to the tut.

  6. #5
    Join Date
    Nov 2009
    Location
    London
    Posts
    866
    Blog Entries
    3
    Rep Power
    14

    Re: Create a easy calculator

    Nice tutorial, +rep!

  7. #6
    Join Date
    Nov 2009
    Location
    I seem to be lost...
    Posts
    1,566
    Blog Entries
    1
    Rep Power
    22

    Re: Create a easy calculator

    Nice tut +rep.
    Knowledge: Intermediate C#, Beginner AS3, HTML, CSS, Binary, Hex, Octal.
    Science is only an educated theory, which we cannot disprove.

  8. #7
    Join Date
    Sep 2009
    Location
    USA
    Posts
    3,400
    Blog Entries
    5
    Rep Power
    37

    Re: Create a easy calculator

    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)

  9. #8
    whitey6993's Avatar
    whitey6993 is offline Programming Expert
    Join Date
    Dec 2008
    Posts
    435
    Rep Power
    15

    Re: Create a easy calculator

    Good tutorial. +rep

  10. #9
    Bartimäus's Avatar
    Bartimäus is offline Programming Expert
    Join Date
    Dec 2009
    Location
    Germany
    Posts
    490
    Rep Power
    11

    Re: Create a easy calculator

    Quote Originally Posted by Guest View Post
    So you basically made the C++ version of my calculator.
    Yes sorry! I didn't see yours at this time! I think it's already very difficult
    to write a beginner tutorial which isn't already in this forum.

  11. #10
    Join Date
    Sep 2009
    Location
    USA
    Posts
    3,400
    Blog Entries
    5
    Rep Power
    37

    Re: Create a easy calculator

    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)

+ Reply to Thread
Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 3
    Last Post: 11-01-2011, 09:47 AM
  2. Maybe a little TOO easy?
    By chrisbregg in forum Python
    Replies: 1
    Last Post: 01-26-2011, 10:02 AM
  3. [help]400$, the easy way.
    By AdvMutant in forum The Lounge
    Replies: 4
    Last Post: 11-29-2010, 11:01 AM
  4. Replies: 0
    Last Post: 07-23-2010, 03:41 AM
  5. Should be a easy fix.. Help please
    By Whitey in forum PHP Development
    Replies: 4
    Last Post: 03-25-2008, 10:35 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts