#If-Else Statement with User Input

8 messages · Page 1 of 1 (latest)

shrewd glen
#
        // CHANGED FROM SECOND PART TO IF-ELSE TO HANDLE EMPTY USER INPUT CASE
        fmt.Print("Time Start in Zulu (HHMMSS): ")
    fmt.Scanln(&timeStart)

    if timeStart != "" {
        test := timeStart
        test1, _ := strconv.Atoi(test)
    } else {
        test := 000000
        test1, _ := strconv.Atoi(test)
    }

        // WORKING SECOND PART (FOR REFERENCE)
    fmt.Print("Time End in Zulu (HHMMSS): ")
    fmt.Scanln(&timeEnd)
    test3 := timeEnd
    test4, _ := strconv.Atoi(test3)
        
        // FOR LOOP THAT IS EVALUATED AFTER TAKING THE USER INPUT // WAS ABLE TO SEE TEST1 BEFORE
    for x := test1; x <= test4; x++ {

Error: Declared and not used

I am attempting to take user input and pass this to a for loop later in the program. Without the if-else statement, it is fine, but as soon as it goes into the if-else statement, it magically can't see it anymore. Any idea what I am missing? I know the variable names can be changed, just POC for now lol

grand lance
#

:= is initializing and assigning to the variable, and it's only available inside that scope

shrewd glen
#

Ah

grand lance
#

var test1 int outside the if-else would fix it, for example

#

And then := -> =

#

Based on your example, though, it seems like the else is redundant

#

var test1 int would default to 0 and then only require an if

shrewd glen
#

That worked! Thank you so much! I need to play around with it some more, the leading zeros are needed unfortunately and that was the only way I can keep the zeros from just wiping and becoming 0