# Project 2: Calculator

> Discover how to build a simple arithmetic calculator with Dart. This step-by-step guide breaks down code into simple English, explaining user input, basic data types, and control structures in Dart. Suitable for beginners!

- URL: https://www.appwriters.dev/projects/dart/project2
- Series: Dart projects

---

Welcome back! Today, we're going to turn lines of code into a simple arithmetic calculator. The aim of this project is to let you engage with Dart's input/output mechanisms, understand basic data types and control structures. 

## The Code

```dart
import 'dart:io';

void main() {
  print('Enter the first number:');
  double num1 = double.parse(stdin.readLineSync()!);

  print('Enter the second number:');
  double num2 = double.parse(stdin.readLineSync()!);

  print('Choose an operation (+, -, *, /):');
  String operation = stdin.readLineSync()!;

  double result;

  switch (operation) {
    case '+':
      result = num1 + num2;
      break;
    case '-':
      result = num1 - num2;
      break;
    case '*':
      result = num1 * num2;
      break;
    case '/':
      result = num1 / num2;
      break;
    default:
      print('Invalid operation.');
      return;
  }

  print('Result: $result');
}
```

## Getting Started

```dart
import 'dart:io';
```
Just like you'd pack a suitcase with everything you need for a trip, this line is packing our program with the necessary tools, specifically the `dart:io` library. This library allows our program to interact with the system's input and output.

## The Main Function

```dart
void main() {
    // Code goes here
}
```
This is the main function, the starting point of our Dart program. Just like the ignition key to a car, it's here where our program starts running.

## Taking User Inputs

```dart
  print('Enter the first number:');
  double? num1 = double.tryParse(stdin.readLineSync() ?? '');

  print('Enter the second number:');
  double? num2 = double.tryParse(stdin.readLineSync() ?? '');

  if(num1 == null || num2 == null) {
    print('Invalid input.');
    return;
  }
```
These lines prompt the user to input two numbers, which we'll perform operations on later. `stdin.readLineSync()` is like our program's ear, listening for input from the keyboard. `double.tryParse()` is the brain interpreting that input as a decimal number (double). The `?? ''` assures Dart that if the user doesn't provide input, default is `''` empty string.

The check for `null` is to ensure that the user has provided valid input. If the input is invalid, we print "Invalid input." and stop the execution of the program.

## Choosing an Operation

```dart
  print('Choose an operation (+, -, *, /):');
  String operation = stdin.readLineSync() ?? '';
```
Now, we're asking the user to choose an operation. Just like in the previous step, we're taking the input and interpreting it as a string.

## Performing the Calculations

```dart
  double result;

  switch (operation) {
    case '+':
      result = num1 + num2;
      break;
    case '-':
      result = num1 - num2;
      break;
    case '*':
      result = num1 * num2;
      break;
    case '/':
      result = num1 / num2;
      break;
    default:
      print('Invalid operation.');
      return;
  }
```
This is the heart of our calculator, where the mathematical magic happens. We use a switch statement, like a multi-path junction, to decide which operation to perform based on the user's input. If none of the cases match the operation, the default path is followed, printing "Invalid operation." and stopping the execution.

## Displaying the Result

```dart
  print('Result: $result');
```
Lastly, we print out the result. Think of this like the final curtain call at the end of a performance - our code's grand finale!

## Conclusion

And that's it! We've turned lines of code into a simple arithmetic calculator. This humble calculator is a stepping stone to more complex projects. With each line of code, we're not only building programs but also enhancing our understanding of Dart.

Stay tuned for our next adventure in Dart programming tomorrow. Happy coding!
