Basic Command Line Calculator

A basic C++ command line calculator for beginners performing addition, subtraction, multiplication, and division with error handling and simple input.

Brief Introduction

Basic Command Line Calculator (cpp)
DESCRIPTION:
A simple C++ program that allows performing basic arithmetic operations (addition, subtraction, multiplication, division) from the command line. The program asks the user for two numbers and an operator, then prints the result. It is an introductory example for those starting to program in C++.

ADDITIONAL NOTES:
The program is designed to be simple and educational, suitable for beginners. It handles user input with basic checks and reports errors such as division by zero or invalid operator.

EXPLANATIONS:
- 1. Inclusion of the iostream library for standard input/output.
- 2. Use of the std namespace to simplify code writing.
- 3. Declaration of variables for the two numbers and the operator.
- 4. Keyboard input for the two numbers and the operator.
- 5. Use of a switch structure to select the operation to perform.
- 6. Handling division by zero error.
- 7. Printing the result or error message.
- 8. The program ends returning 0, indicating correct execution.

Example Code: cpp

#include <iostream>

// Basic command line calculator
// Allows performing addition, subtraction, multiplication, and division

int main() {
    double num1, num2, risultato;
    char operatore;

    std::cout << "Enter the first number: ";
    std::cin >> num1;

    std::cout << "Enter the operator (+, -, *, /): ";
    std::cin >> operatore;

    std::cout << "Enter the second number: ";
    std::cin >> num2;

    // Perform the operation based on the entered operator
    switch (operatore) {
        case '+':
            risultato = num1 + num2;
            break;
        case '-':
            risultato = num1 - num2;
            break;
        case '*':
            risultato = num1 * num2;
            break;
        case '/':
            if (num2 == 0) {
                std::cerr << "Error: division by zero is not allowed." << std::endl;
                return 1; // error code
            }
            risultato = num1 / num2;
            break;
        default:
            std::cerr << "Error: invalid operator." << std::endl;
            return 1; // error code
    }

    std::cout << "Result: " << risultato << std::endl;

    return 0;
}
×
×
×
Do you want to regenerate the answer?
×
Export chat
Choose export format
Generazione PDF in corso…
Allegati
×
You are about to close the chat and switch to the image generator. If you are not logged in, you will lose our chat. Do you confirm?
You are viewing a shared chat in temporary mode. It will not be saved.
×
Saved prompts
×
Private note
×
Label
×
Search all chats
×
Your insights
Analyzing…
×
AI Preferences
Assistant name 30
Answer length
AI model
Structured answers
Include examples
Use emojis
Guide me, don't solve Hints and questions instead of a ready answer
More settings →
×
Share this chat
Anyone opening this link can view the chat or add it to their profile as their own chat.
Note: the chat attachments will be shared too. Anyone who adds it will receive a copy of the files in their own folder.
Shared chat
Someone shared a chat with you. Do you want to just view it or add it to your chats?
Note: the chat attachments will be shared too. Anyone who adds it will receive a copy of the files in their own folder.
×

Saved messages

Loading...

×

Chat History

example-programs · CHAT HISTORY

Loading...

AI Preferences

×
  • BasicQuick and essential answers for study
  • MediumHigher quality for study and programming
  • AdvancedComplex reasoning and detailed analysis
Explain Steps

What do you need?

Software:
  • Standard C++ language (compatible with C++11 and later)
  • No external libraries required
  • C++ compiler (g++, clang++, MSVC)
Configuration:
  • No special configuration needed
Hardware:
  • Computer with terminal or console for input/output
Notes on Arduino wiring/pinout:
  • Not applicable (command line program on PC)