Home > Software design >  I'm having dificulties with output problems in my Go code
I'm having dificulties with output problems in my Go code

Time:12-10

I'm new here and to programming. I am trying to improve my programming logic by doing some exercises in my preferred languages such as Python and Go.

In this particular case, my issue is with Go.

The code basically should print the following:

Example

First person info:
Name: Mary
Age: 19
Second person info:
Name: John
Age: 20
The average age of Mary and John is 19.5 years

To solve this exercise, I tried the following code:

package main

import "fmt"

func main() {
    var name1 int
    var name2 int
    var age1, age2 int
    var average float32
    fmt.Println("First person info:")
    fmt.Print("Name: ")
    fmt.Scan(&name1)
    fmt.Print("Age: ")
    fmt.Scan(&age1)
    fmt.Println("Second person info:")
    fmt.Print("Name: ")
    fmt.Scan(&name2)
    fmt.Print("Age: ")
    fmt.Scan(&age2)

    average = float32((age1   age2) / 2)
    fmt.Printf("\nThe average age of %d and %d is %f years", &name1, &name2, &average)

}

I've tried some variations, trying to convert some variables, but the thing is that the output is like below:

First person info:
Name: Mary
Age: Second person info:
Name: Age:
The average age of %s and %s is %.2f years 0xc000018088 0xc0000180a0 0xc0000180b8
PS C:\Users\alexi\Meu Drive\Programação\golang> 

So, the code don't show the print function one by one. I answer the first name (name1) and soon I answer it, the output brings all others, all together (age, second person info etc.) I tried everything and can't understand what possibly went wrong...

CodePudding user response:

A couple of things:

  1. The fmt.Print function does not add a newline character to the string you're printing. So you probably want to change lines like fmt.Print("Name: ") to fmt.Println("Name:") or fmt.Print("Name:\n").
  2. #1 will help here, but you may want to look at the fmt.Scanf function. It lets you provide a format string like this fmt.Scanf("%d", &age1) to explicitly look for an integer or whole number in the input.
  3. You've declared the name1 and name2 variables as int. I think they should be string.
  4. Remove the & before the variables in the line where you print out the average. It's not needed there.

Hopefully, that gets you closer to where you want to be.

CodePudding user response:

package main

import (
"fmt"
"strconv"
)


func main() {

var name1, age1 , name2, age2 string
var average float32

fmt.Println("First person info:")
fmt.Print("Name: ")
fmt.Scan(&name1)
fmt.Print("Age: ")
fmt.Scan(&age1)
fmt.Println("Second person info:")
fmt.Print("Name: ")
fmt.Scan(&name2)
fmt.Print("Age: ")
fmt.Scan(&age2)

ageInt1, _ := strconv.Atoi(age1)
ageInt2, _ := strconv.Atoi(age2)

average = float32(( ageInt1  ageInt2) / 2)
fmt.Printf("\nThe average age of %v and %v is %f years", name1, name2, average)


}

//output will be like
//enter code here
//First person info:
//Name: Joh
//Age: 23
//Second person info:
//Name: Doe
//Age: 34

//The average age of Joh and Doe is 28.000000 years% 
  • Related