3

I am currently learning Go lang. Trying it on different platforms: Linux, Windows When I run code on Linux it runs perfectly, but when I try this program on Windows it doesn't work.

Its just simple cmd calculator which allows simple operations like add number, multiply eg. Its not handling wrong input like characters. It's my first program for adoption Go syntax

What doesn't work:

  1. Parsing int
  2. Comparing input

Code:

package main

import (
    "bufio"
    "fmt"
    "math"
    "os"
    "strconv"
    "strings"
)

func main() {

    reader := bufio.NewReader(os.Stdin)
    var operation int
    var firstNumber float64
    var secondNumber float64

    fmt.Println("Simple cmd calculator")

    repeat := true

    for repeat {

        fmt.Println("Enter number 1: ")
        firstNumber = getNumber(*reader)

        fmt.Println("Enter number 2: ")
        secondNumber = getNumber(*reader)

        fmt.Println()

        selectOperation(*reader, &operation)

        fmt.Print("You result is: ")

        switch operation {
        case 1:
            fmt.Println(add(firstNumber, secondNumber))
        case 2:
            fmt.Println(subtract(firstNumber, secondNumber))
        case 3:
            fmt.Println(multiply(firstNumber, secondNumber))
        case 4:
            fmt.Println(divide(firstNumber, secondNumber))
        }

        fmt.Println("Do you want to continue? [Y/n]")
        input, _ := reader.ReadString('\n')

        input = strings.Replace(input, "\n", "", -1)

        if !(input == "Y" || input == "y") {
            repeat = false
        }

    }

}

func selectOperation(reader bufio.Reader, operation *int) {
    fmt.Println("1. Add")
    fmt.Println("2. Subtract")
    fmt.Println("3. Multiply")
    fmt.Println("4. Divide")

    fmt.Print("Select operation: ")
    input, _ := reader.ReadString('\n')
    input = strings.Replace(input, "\n", "", -1)
    number, _ := strconv.Atoi(input)
    *operation = number
}

func getNumber(reader bufio.Reader) float64 {

    input, _ := reader.ReadString('\n')
    input = strings.Replace(input, "\n", "", -1)
    convertedNumber, _ := strconv.ParseFloat(input, 64)
    return convertedNumber

}

func add(a float64, b float64) float64 {
    return (math.Round((a+b)*100) / 100)
}

func subtract(a float64, b float64) float64 {
    return (math.Round((a-b)*100) / 100)
}

func multiply(a float64, b float64) float64 {
    return (math.Round(a*b*100) / 100)
}

func divide(a float64, b float64) float64 {
    return (math.Round(a/b*100) / 100)
}

Results:

Linux

Windows

Am I doing something wrong or it's not my bad?

5
  • On windows it's likely to have \r\n as the string delimiter, confirm it with fmt.Printf("% x", input) Commented Nov 9, 2018 at 21:32
  • But it doesn't work on linux. So I have to replace it twice? Commented Nov 9, 2018 at 21:36
  • 1
    Remove both \n and \r Commented Nov 9, 2018 at 21:43
  • stackoverflow.com/questions/14493867/… Commented Nov 9, 2018 at 21:44
  • 1
    Please include the output in the question, formatted as code, rather than as links to images. Commented Nov 9, 2018 at 21:55

1 Answer 1

3

Thanks for help from @zerkms.

Answer is:

input = strings.Replace(input, "\r", "", -1)
input = strings.Replace(input, "\n", "", -1)

Now it will work properly both on windows and linux

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.