First C++ code

As stated in the introductory text on the Home page of this website, I recently began to familiarize myself with programming, notably C++. The first thing I did is to get through the most important topics of the C++ syntax on cplusplus.com, which is a great site for later reference as well. It is by far not enough to grasp all the the capabilities of the language, but it is one point to start at. After getting an overview of the language on that website, I switched to a book (by Bjarne Stroustrup) that actually is written for beginners. After just getting through a few chapters, I could already write something like you can see down below. It is not much, but after the classic “Hello World!” program, it is the first thing that actually can do something useful.

OK, let’s see some (extra basic) code:

C++

// my very first program, a simple calculator

#include <iostream>

using namespace std;

int main() {
    char operation;
    double operand1, operand2, result;
    bool good_operation = false;
    
    cout << "Specify operation (+,-,*,/) and operands in the following format: 1+1\n";
    cin >> operand1 >> operation >> operand2;

// This is too 'if'y. Use 'switch' instead. Also, it is harder to handle
// erroneous operations with this approach.  
//  if (operation == '+') {result = operand1+operand2;}
//  if (operation == '-') {result = operand1-operand2;}
//  if (operation == '*') {result = operand1*operand2;}
//  if (operation == '/') {result = operand1/operand2;}
//  cout << "Result of " << operand1 << operation << operand2 << " is " << result << "\n";
    
    
    while (good_operation==false) {
        switch (operation) {
            case'+':
                result = operand1+operand2;
                cout << "Result of " << operand1 << operation
                    << operand2 << " is " << result << "\n";
                good_operation = true; // lack of this results in infinite loop
                break; // lack of this results in executing next case too
            case'-':
                result = operand1-operand2;
                cout << "Result of " << operand1 << operation
                    << operand2 << " is " << result << "\n";
                good_operation = true;
                break;
            case'*':
                result = operand1*operand2;
                cout << "Result of " << operand1 << operation
                    << operand2 << " is " << result << "\n";
                good_operation = true;
                break;
            case'/':
                result = operand1/operand2;
                cout << "Result of " << operand1 << operation
                    << operand2 << " is " << result << "\n";
                good_operation = true;
                break;
            default:
                cout << "Unrecognized operation. Please enter a correct one (+,-,*,/):\n";
                cin >> operation;
        }
    }
    return 0;
}