My calculator program isnt working

Hi
This program is a calculation program. Calculates how many bars i can cut on a 4 meters length and how many i need to buy. Anyway it works to line 20 then its there it asks how many bars i need and what i typ in there lets say 21 i get in the last line of code where it should show the result i dont know what i have done wrong.

package main

import (
    "fmt"
    "math"
)

func main() {
    for {
		fmt.Println("what is your cut length? In cm: ")

		var cutlength int
		fmt.Scan(&cutlength)

		barlength := 400
		lengtheverybar := barlength / cutlength
		cutbars := math.Floor(float64(lengtheverybar))
                fmt.Println("You will get", cutbars, "every 4 meters bar.")

		fmt.Println("How many cut bars do you need? In pieces: ")

		var lengthpercut int
		fmt.Scan(&lengthpercut)

                totallength := lengthpercut * cutlength
                fmt.Println(totallength)
                barstobuy := totallength / cutlength
                fmt.Println(barstobuy)
                roundbarstobuy := math.Ceil(float64(barstobuy))

               fmt.Println("You have to buy", roundbarstobuy, "4 meters bars.")
    }
}

Hi, @Michael_taradus,

When I run your program, I get:

PS C:\Users\skillian\source\repos\gopath\src\forum.golangbridge.org\my-calculator-program-isnt-working_19921> go run .
what is your cut length? In cm: 
40
You will get 10 every 4 meters bar.
How many cut bars do you need? In pieces:
16
640
16
You have to buy 16 4 meters bars.
what is your cut length? In cm:

Is that what you’re getting? If not, can you post the output you get? If so, can you provide an example of what you’d like to see instead?

yes its what i get. But the program is skipping this two lines

            barstobuy := totallength / cutlength
            roundbarstobuy := math.Ceil(float64(barstobuy))

The end result should be like “You have to buy 2 4 meters bars.” if it wasnt skipping those lines.

After these lines:

totallength := lengthpercut * cutlength
fmt.Println(totallength)
barstobuy := totallength / cutlength

Then barstobuy is equal to lengthpercut

Why? shouldent barstobuy be equal to what ever the result is when this two is divided?
totallength / cutlength

Because thats what im intended with that line first it calculate totallength then it should calculate barstobuy and before it presents the end result it should round it up and present the result with this roundbarstobuy.

So to compute totallength, you’re multiplying lengthpercut by cutlength.

Then to compute barstobuy, you’re taking that totallength and dividing it by cutlength. You could rewrite:

totallength := lengthpercut * cutlength
barstobuy := totallength / cutlength

as

barstobuy := (lengthpercut * cutlength) / cutlength

So the multiply and then divide by cutlength is pointless. You could just write barstobuy := lengthpercut.

I think what you might want is something like

var cutsneeded int
fmt.Scan(&cutsneeded)

barstobuy := math.Ceil(float64(cutsneeded)/float64(lengtheverybar))
1 Like

Hi
Its my mistake. I wrote this program in python and now im trying to convert it to go :slight_smile: to learn the language.

It should be divided by barlength. So the formula should be like this
lengtheverybar * cutsneeded / barlength

I tried this but i got wrong results.
barstobuy := math.Ceil(float64(lengtheverybar) * float64(cutsneeded) / float64(barlength))

nvm got it to work it was a mixup in the strings.

This is the correct solution.

barstobuy := math.Ceil((float64(cutsneeded) * float64(cutlength)) / float64(barlength))