Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Solution: calculator STDIN

package main

import (
	"fmt"
	"os"
	"strconv"
)

func main() {
	var aStr string
	var bStr string
	var operator string
	var a float64
	var b float64
	var err error
	var result float64

	fmt.Print("a: ")
	fmt.Scan(&aStr)
	a, err = strconv.ParseFloat(aStr, 64)
	if err != nil {
		fmt.Printf("The value '%v' could not be converted to a floating point number. %v\n", aStr, err)
		os.Exit(1)
	}

	fmt.Print("op: ")
	fmt.Scan(&operator)

	fmt.Print("b: ")
	fmt.Scan(&bStr)
	b, err = strconv.ParseFloat(bStr, 64)
	if err != nil {
		fmt.Printf("The value '%v' could not be converted to a floating point number. %v\n", bStr, err)
		os.Exit(1)

	}

	if operator == "+" {
		result = a + b
	} else if operator == "-" {
		result = a - b
	} else if operator == "*" {
		result = a * b
	} else if operator == "/" {
		result = a / b
	} else {
		fmt.Printf("Unhandled operator: '%v'\n", operator)
		os.Exit(1)
	}

	fmt.Printf("%v %v %v = %v\n", a, operator, b, result)

}