When I am using GoLand to run my program, everything works fine, just like what I expected. But, my program does not work well when I try to manually run it in CMD terminal.
The command line I used to run my program was "go run c.go" where c.go is my source file.
Here is the correct result from GoLand:
Where the green numbers are the inputs from stdin.
Meanwhile with the terminal:
The program failed after I input "-1" into stdin.
Here is the code:
func IterElements(numElement int, elements []string) int {
if numElement == 0 {
return 0
}
//********************************
var e = elements[0] //Line 22
//********************************
number, err := strconv.Atoi(e)
if err != nil {
//fmt.Println(err)
//os.Exit(2)
}
numElement--
if number<0{
return IterElements(numElement, elements[1:])
}else {
return number*number + IterElements(numElement, elements[1:])
}}
It converts a string from elements-array to an integer and calculates its square, and sum the squared numbers iteratively.
Any help? Thank you!
Update: Full Code in Playground