HOW TO CREATE A CALCULATOR

Image result for calculator


  • 2
    Declare Headers. Begin writing in source file, using #include declare headers iostream and iomanip and using the identifier, using namespace declare std;.
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    1. 3
      Create main. Create the main statement you are going to write the code in. Add the return statement at end of code in the main function.
      int main()
      {
      return 0;
      }
      
    2. 4
      Create variables. Begin code between brackets of the main statement before the return statement. Declare int variables num1 and num2, and char variable opr.
      int num1, num2;
      char opr;
      
    3. 5
      Get values for num1 and num2. Use the cout command to prompt the user to enter two numbers. Using cin Assign the input to variables num1 and num2.
      cout << "Enter two integers: ";
      cin >> num1 >> num2;
      
    4. 6
      Get operator for opr. Use the cout command to prompt user to enter an operator for the equation. Using cin assign the input to the char variable, opr.
      cout << "Enter operator: + (addition), - (subtraction)," << "
       * (multiplication), / (division): ";
      cin >> opr;
      cout << endl;
      
    5. 7
      Create output statement. Use cout to output results of what was entered then begin switch statement to find the result.
      cout << num1 << " " << opr << " " << num2 << " = ";
      switch (opr){
      }
      
    6. 8
      Declare case ‘+’. Make case statement for when the user wants to do addition using case, use cout to output the product of num1 + num2, end the case with break.
      case '+':
           cout << num1 + num2 << endl;
               break;
      
    7. 9
      Declare case ‘-’. Make case statement for when the user wants to do subtraction using case. Use cout to output the product of num1 - num2, and end the case with break.
      case'-':
          cout << num1 - num2 << endl;
              break;
      
    8. 10
      Declare case ‘*’. Make case statement for when the user wants to do multiplication using case. Use cout to output the product of num1 * num2, and end the case with break.
      case'*':
          cout << num1 * num2 << endl;
              break;
      
    9. 11
      Declare case ‘/’. Make case statement for when the user wants to do division. For this case though you have to use an if and else statement in case the user tries to divide by zero, if the number is not zero use cout to output the product of num1 / num2, else if it is zero use cout to output a statement letting the use know the problem.
      case'/':
      if (num2 != 0)
          cout << num1 / num2 << endl;
      else
          cout << "ERROR \nCannot divide by zero" << endl;
              break;
      
    10. 12
      Add a default statement. Include the default statement within the switch structure. Default statement lets the user know when variables enter are not the correct operators. End the switch after the default statement.
      default:
      cout << "Illegal operation" << endl;
      }
      
    11. 13
      Run the program. Go to the build menu on the top of the screen and click build program, then press ctrl 5 on the keyboard to run it. If there are errors the compiler will show their location.
      • Here is the final code: 
        #include <
        >
        #include <iomanip>
        using namespace std;
        int main()
        {
        int num1, num2;
        char opr;
        cout << "Enter two integers: ";
        cin >> num1 >> num2;
        cout << endl;
        cout << "Enter operator: + (addition), - (subtraction)," << " * (multiplication), / (division): ";
        cin >> opr;
        cout << endl;
        cout << num1 << " " << opr << " " << num2 << " = ";
        switch (opr){
        case '+':
        cout << num1 + num2 << endl;
        break;
        case'-':
        cout << num1 - num2 << endl;
        break;
        case'*':
        cout << num1 * num2 << endl;
        break;
        case'/':
        if (num2 != 0)
        cout << num1 / num2 << endl;
        else
        cout << "ERROR \nCannot divide by zero" << endl;
        break;
        default:
        cout << "Illegal operation" << endl;
        }
        return 0;
        }
        
        
        
        
        CREDITS TO: WIKIHOW 

    Comments

    1. As a member of the Comment Reading Association of Philippines (CRAP) , I'm only here to read comments. Please keep the comments short and simple. We do appreciate your typing efforts. Also please watch your spelling. Thank you.

      ReplyDelete
    2. Thank you for this info. Where did you learn it?

      ReplyDelete
      Replies
      1. i learn this in my prof thank you for your comment

        Delete
    3. i learn many thing from your blog. how did you do that?

      ReplyDelete
    4. wow.It's nice. Do you I can do that ?

      ReplyDelete
    5. Its nice but my question is what the program we use ?? E.g phyton or c++??

      ReplyDelete
    6. Thank you for this wonderful info. Is this program really good?

      ReplyDelete
    7. Thank you for this info. Where did you learn it?

      ReplyDelete
    8. Where's the step number 1? Am I blind or what?

      ReplyDelete
    9. Thank you for this info. Where did you learn it?

      ReplyDelete
    10. Thank you for this information. Why did you share this kind of information? Do you think some Computer Programmers will benefit from this?

      ReplyDelete
    11. Nice where did you get the info?

      ReplyDelete
    12. thank you for this information. Is this calculator from application or software? or real calculator?

      ReplyDelete
    13. Nice info. Is this useful to non-programming students?

      ReplyDelete
    14. Thank you for this info. Where did you learn it?

      ReplyDelete

    Post a Comment