S
S
Stas Vinokur2019-01-09 18:58:34
go
Stas Vinokur, 2019-01-09 18:58:34

How to check a string for the presence of a digit, if there are no digits write an error and close the program?

func main() {
  var num = 0
  var summa = 0
  var ispos = 0
  fmt.Print("Введите число: ")
  fmt.Fscan(os.Stdin, &num)
  fmt.Println(num)

  if num < 0 {
    num = num * -1
    ispos = -1
    fmt.Println(num)
  }
  for i := 1; i <= num; i++ {
    fmt.Println(i)
    summa = summa + i
  }
  if ispos == -1 {
    summa = summa * ispos
  }
  fmt.Println(summa)
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vasily Melnikov, 2019-01-09
@BacCM

You read a line from a stream.
then
num, err := strconv.Atoi
in num number in err error if string is not a valid number

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question