Skip to main content ppwriters

#DartProjects

Project 2

Building A Simple Arithmetic Calculator

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

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

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

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

  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

  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

  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

  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!

Enjoying? Tell your friends.

#DartProjects

Learn the basics of Dart by building projects. Learn to build various projects step by step using Dart along the way brush up your Dart programming skills.

back to Dart projects

Join our community on Discord to connect with fellow learners, share your progress, and get help with any questions you may have throughout the #DartProject challenge. Join now and get started on your journey to mastering Dart!

join now