Placeholders


package main
import "fmt"

func main() {
    var first_name string
    var last_name string
    var grade int
    var student_id int
    var login string
    var gpa float64

    fmt.Println("Please enter the following information:")
    fmt.Print("First name: ")
    fmt.Scanf("%s", &first_name)
    fmt.Print("Last name: ")
    fmt.Scanf("%s", &last_name)
    fmt.Print("Grade (9 - 12): ")
    fmt.Scanf("%d", &grade)
    fmt.Print("Student ID: ")
    fmt.Scanf("%d", &student_id)
    fmt.Print("Log-in: ")
    fmt.Scanf("%s", &login)
    fmt.Print("GPA (0.0 - 4.0): ")
    fmt.Scanf("%f", &gpa)

    fmt.Println("Your information:")
    fmt.Printf("\tLogin: %s\n", login)
    fmt.Printf("\tID: %s\n", student_id)
    fmt.Printf("\tName: %s %s\n", first_name, last_name)
    fmt.Printf("\tGPA: %f\n", gpa)
    fmt.Printf("\tGrade: %d\n", grade)
}




...But wait! There's more...

package main
import "fmt"

// one verison all in main...
// another version uses a different func

func main() {
    var name string
    var age int
    var age_back int
    var age_plus int

    fmt.Println("Hello. What is your name?")
    fmt.Scanf("%s", &name)
    fmt.Printf("Hi %s! How old are you? ", name)
    fmt.Scanf("%d", &age)

    age_back = age - 5
    fmt.Printf("Did you know that five years ago, you were be %d years old?\n", age_back)
    age_plus = age + 5
    fmt.Printf("...and that five years from now, you will be %d years old. Imagine that!\n", age_plus)
}

* This was how I solved one exercise from Graham Mitchell's Programming By Doing website.

2 comments:

  1. Example two has a bug for me:

    *The scanf for the integer is ignored by full execution.
    * If I time it to wait, it claims the input integer to be an invalid input command.

    ReplyDelete
  2. I just discovered your posts the other day and they were pretty good. I wanted to send a few to some coworkers but really can you take down the pictures of the women?

    They're really quite inappropriate. The "brogrammer" culture is bad enough in tech as it is and they also make ZERO sense.

    I've got two young boys myself and I really don't feel like this is a message we need to be sending.

    I would have sent this privately but couldn't find an alternate contact method.

    Thanks

    ReplyDelete