I was attempting to run a program that created a new instance of a predefined struct from the users input.
The struct is comprised of 3 fields type string. The goal being for the user to input strings into a scanner, creating a new, unique instance of the struct, using the input as it's values. The program would be in a loop, allowing the user to create multiple instances of the struct.
In the code presented, there are 2 struct types; Boat and Car. The first string input would indicate which type of struct is chosen to be created, and the next 3 strings would be to fill the struct values.
package main
import(
"fmt"
"bufio"
"os"
)
type Boat struct {
name string
color string
design string
}
type Car struct {
name string
color string
design string
}
func main() {
TempBoat := Boat{"gerrard","red","speed"}
TempCar := Car{"conroy","blue","cruiser"}
/* I was using a template that would then be filled by the user, but
this only allows for one instance that would continue to be
overwritten. */
scanner := bufio.NewScanner(os.Stdin)
for {
if scanner.Scan() {
userIn := scanner.Text()
scanMain := strings.Fields(userIn)
Scan0 := scanMain[0]
Scan1 := scanMain[1]
Scan2 := scanMain[2]
Scan3 := scanMain[3]
if Scan0 == "car" {
TempCar.name = Scan1
TempCar.color = Scan2
TempCar.design = Scan3
} else if Scan0 == "boat" {
TempBoat.name = Scan1
TempBoat.name = Scan2
TempBoat.name = Scan3
} else {
fmt.Println("Invalid Input. Try Again.")
}
}
}
}