Can now add to vector, still practicing channels

This commit is contained in:
raul 2024-03-06 18:04:34 +01:00
parent a3b8027714
commit 91d4985651
1 changed files with 29 additions and 3 deletions

View File

@ -3,6 +3,7 @@ package main
import ( import (
"fmt" "fmt"
"strconv" "strconv"
"time"
) )
var numsToSum []int var numsToSum []int
@ -13,24 +14,49 @@ func getAverage() {
} }
func addToVec(num int, c chan int) {
numsToSum = append(numsToSum, num)
}
func statusMSG() { func statusMSG() {
fmt.Printf("Current numbers: %v\n", numsToSum) fmt.Printf("Current numbers: %v\n", numsToSum)
fmt.Println("[1] Add number") fmt.Println("[1] Add number")
fmt.Println("[2] Calculate average") fmt.Println("[2] Calculate average")
fmt.Println("[3] Exit")
} }
func fun2() { func getChoice2() {
clear()
statusMSG()
strchoice2 := scanLine() strchoice2 := scanLine()
choice2, err = strconv.Atoi(strchoice2) choice2, err = strconv.Atoi(strchoice2)
catchErr(err) catchErr(err)
}
func getNumToAdd() (num int) {
fmt.Printf("Select your number: ")
strnum := scanLine()
num, err = strconv.Atoi(strnum)
return num
}
func fun2() {
c := make(chan int)
looper: looper:
for { for {
clear()
statusMSG()
fmt.Printf("Choice: ")
getChoice2()
switch choice2 { switch choice2 {
// TODO: Add options to add numbers to array and then to calcluate them later // TODO: Add options to add numbers to array and then to calcluate them later
case 1: case 1:
numToAdd := getNumToAdd()
addToVec(numToAdd, c)
fmt.Printf("Succesfully added %v to the array\n", numToAdd)
time.Sleep(time.Second)
case 2:
getAverage()
case 3:
break looper break looper
} }
} }