High Order Functions issue

When following the High Order Functions lesson and using the code provided:

package main

import "fmt"

func calcArea(r float64) float64 {
	return 3.14 * r * r
}
func calcPerimeter(r float64) float64 {
	return 2 * 3.14 * r
}
func calcDiameter(r float64) float64 {
	return 2 * r
}

func main() {
	var query int
	var radius float64

	fmt.Print("Enter the radius of the circle: ")
	fmt.Scanf("%f", &radius)
	
	fmt.Printf("Enter \n 1 - area \n 2 - perimeter \n 3 - diameter: ")
	fmt.Scanf("%d", &query)

	if query == 1 {
		fmt.Println("Result: ", calcArea(radius))
	} else if query == 2 {
		fmt.Println("Result: ", calcPerimeter(radius))
	} else if query == 3 {
		fmt.Println("Result: ", calcDiameter(radius))
	} else {
		fmt.Println("Invalid query")
	}
}

the program executes like this:

go run .\main.go
Enter the radius of the circle: 10
Enter
 1 - area
 2 - perimeter
 3 - diameter: Invalid query

not asking for the query var input.
I’ve tried to find the issue but to no avail.

Hi @jordanmatyka

The query requires any integer in 1, 2, or 3. If you enter any other than these 3, you would get Invalid query returned.

This print statement can be improved for readability:
fmt.Printf("Enter \n 1 - area \n 2 - perimeter \n 3 - diameter: ")

Something like this, maybe:

	fmt.Println("Select your query")
	fmt.Printf(" 1 - area \n 2 - perimeter \n 3 - diameter: \n Enter:")

That’s not the issue.
The program doesn’t ask for 2nd input (query) if a value for radius is given:

  1. Program is run and asks for radius
go run .\main.go
Enter the radius of the circle: 10

I input 10 and press enter
2. After, this is printed:

Enter 
 1 - area
 2 - perimeter
 3 - diameter: Invalid query

And the program exits.

Now:
If I don’t provide the radius on the first input prompt and just hit enter, this happens:

go run .\main.go
Enter the radius of the circle: 
Enter 
 1 - area
 2 - perimeter
 3 - diameter: 3
Result:  0

Can you please share the lab url?

I’ve tested this function outside the lab on my workstation.

https://learn.kodekloud.com/user/courses/golang/module/71aef76c-bfcd-4e49-926e-fa7fbf73827c/lesson/1d067f98-2f95-452b-a8a7-9bb184d3d8de

The link you provided does not contain the function you described.

However, this query arg as a conditional:

	if query == 1 {
		fmt.Println("Result: ", calcArea(radius))
	} else if query == 2 {
		fmt.Println("Result: ", calcPerimeter(radius))
	} else if query == 3 {
		fmt.Println("Result: ", calcDiameter(radius))
	} else {
		fmt.Println("Invalid query")
	}

So, if you pass any other int that either 1, 2, or 3 each selection calling respective func. If you enter any other int the these three, the result would be Invalid query.

As mentiond earlier, I’ve just updated the print statements for readability and hre’s how it works:


@jordanmatyka @Santosh_KodeKloud

There is a very subtle bug in the original code, and that’s down to the incorrect use of the fmt.Scanf call.

Those lines should read

	fmt.Print("Enter the radius of the circle: ")
	fmt.Scanf("%f\n", &radius)
	
	fmt.Printf("Enter \n 1 - area \n 2 - perimeter \n 3 - diameter: ")
	fmt.Scanf("%d\n", &query)

Note carefully the \n I have added to both format strings. This tells Scanf which is reading the keyboard input to also consume the ENTER keypress. Without this then the ENTER you press to enter the radius remains in the keyboard buffer and is immediately consumed by the second Scanf which is asking for the query number. Because that second one consumes the ENTER key, it returns no number therefore the if statement will go directly to the final else and print “Invalid query”.

As a general rule, if you want to read keyboard input from the user and expect the user to press ENTER, then you must terminate the format string for Scanf with a \n

2 Likes

My attempt to skip the radius above gave me a hint that there’s something wrong with how Scanf is used. Trying to fix this using assisants was giving me solutions awfuly complicated for a beginner (bufio.Reader()) so great to see a much simpler way. Thank you @Alistair_KodeKloud!

nb: The bug is in the course resources here on slide 316:


and in the lesson around 02:45:

@jordanmatyka If you have any other issues with this course or need something explained better, tag me in your post :slight_smile:

1 Like