Skip to content

Commit 6d1551c

Browse files
author
Andrew Edison
committed
marking part 2 work
1 parent 116c6ca commit 6d1551c

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

exercise1/main.go

+19-5
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@ import (
99
"io/ioutil"
1010
"log"
1111
"os"
12+
"time"
1213
)
1314

1415
func main() {
1516
filename := flag.String("filename", "problems.csv", "path to problems csv file")
17+
limit := flag.Int("limit", 30, "time limit in seconds to complete the quiz")
1618
flag.Parse()
1719

1820
// read a csv file from command line, default to problems.csv -filename flag
@@ -49,16 +51,28 @@ func main() {
4951
// read out each question, then read in answer. keep track of how many are correct and wrong
5052
scanner := bufio.NewScanner(os.Stdin) // define our scanner to read stdin
5153
var correct int
54+
timer := time.NewTimer(time.Duration(*limit) * time.Second)
55+
56+
problemLoop:
5257
for _, p := range problems {
5358
fmt.Printf("%s ? >> ", p.Question)
54-
scanner.Scan()
55-
a := scanner.Text()
56-
if a == p.Answer {
57-
correct++
59+
answerCh := make(chan string)
60+
go func() {
61+
scanner.Scan()
62+
answerCh <- scanner.Text()
63+
}()
64+
select {
65+
case <-timer.C:
66+
fmt.Println()
67+
break problemLoop
68+
case answer := <-answerCh:
69+
if answer == p.Answer {
70+
correct++
71+
}
5872
}
73+
5974
}
6075

6176
// output the results at the end
62-
fmt.Println("")
6377
fmt.Printf("You answered %d out of %d questions right. Thanks for playing.\n\n", correct, len(problems))
6478
}

0 commit comments

Comments
 (0)