My calculator program isnt working

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 (

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

		var cutlength int

		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

                totallength := lengthpercut * cutlength
                barstobuy := totallength / cutlength
                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\\my-calculator-program-isnt-working_19921> go run .
what is your cut length? In cm: 
You will get 10 every 4 meters bar.
How many cut bars do you need? In pieces:
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
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


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

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

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))

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.