Download ArticleDownload Article

Looking for a new challenge to build upon your basic C++ coding experience? Start with step one to make a basic addition, subtraction, multiplication, and division calculator using C++ coding.

  1. How.com.vn English: Step 1  Create source file.
    Create source file to start writing C++ program.
  2. How.com.vn English: Step 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;

    Advertisement
  3. How.com.vn English: Step 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;}
  4. How.com.vn English: Step 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;
  5. How.com.vn English: Step 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;
  6. How.com.vn English: Step 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;
  7. How.com.vn English: Step 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){}
  8. How.com.vn English: Step 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;
  9. How.com.vn English: Step 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;
  10. How.com.vn English: Step 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;
  11. How.com.vn English: Step 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;
  12. How.com.vn English: Step 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;}
  13. How.com.vn English: Step 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 <iostream>#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;elsecout << "ERROR \nCannot divide by zero" << endl;break;default:cout << "Illegal operation" << endl;}return 0;}
  14. Advertisement


Community Q&A

Search
Add New Question
  • Question
    How can I use C++ to create scientific calculator?
    How.com.vn English: Community Answer
    Community Answer
    Code is too long to put here. Try asking the question on stackoverflow, where people have the space to write out code. I suggest (if you're really passionate) you learn C++ and make the code yourself. It won't be a hard code (some people write one as they learn C++).
  • Question
    How do I make this calculator output to two decimal places?
    How.com.vn English: Community Answer
    Community Answer
    Press the S-to-D button, or just put a 0 at the end.
  • Question
    Can I make games using C++ coding?
    How.com.vn English: Community Answer
    Community Answer
    Yes. C++ is a very good language to create a game in. Although it will be more difficult than a language like Java, it will be much faster and more efficient.
See more answers
Ask a Question
200 characters left
Include your email address to get a message when this question is answered.
Submit
      Advertisement

      Tips

      • If code will not run make sure ; is after the correct statements and : is after each case.
      • If program is running but answers are not correct make sure break; is after each case statement.
      Submit a Tip
      All tip submissions are carefully reviewed before being published
      Thanks for submitting a tip for review!
      Advertisement

      Things You'll Need

      • Computer
      • C++ compiler (Examples use Visual Basic)

      About This Article

      How.com.vn is a “wiki,” similar to Wikipedia, which means that many of our articles are co-written by multiple authors. To create this article, volunteer authors worked to edit and improve it over time. This article has been viewed 92,104 times.
      How helpful is this?
      Co-authors: 7
      Updated: March 29, 2023
      Views: 92,104
      Thanks to all authors for creating a page that has been read 92,104 times.

      Is this article up to date?

      ⚠️ Disclaimer:

      Content from Wiki How English language website. Text is available under the Creative Commons Attribution-Share Alike License; additional terms may apply.
      Wiki How does not encourage the violation of any laws, and cannot be responsible for any violations of such laws, should you link to this domain, or use, reproduce, or republish the information contained herein.

      Notices:
      • - A few of these subjects are frequently censored by educational, governmental, corporate, parental and other filtering schemes.
      • - Some articles may contain names, images, artworks or descriptions of events that some cultures restrict access to
      • - Please note: Wiki How does not give you opinion about the law, or advice about medical. If you need specific advice (for example, medical, legal, financial or risk management), please seek a professional who is licensed or knowledgeable in that area.
      • - Readers should not judge the importance of topics based on their coverage on Wiki How, nor think a topic is important just because it is the subject of a Wiki article.

      Advertisement