Why Looping Variable "ch" not making condition true


(M A D H U R ) #1

"Hmm looping condition is true but when i update looping variable condition just before end of for loop it not worked".

package main

import “fmt”

func main() {

ch := 'y'
for ch == 'y'{

	fmt.Println("\n\na. Add")
	fmt.Println("b. Sub")
	fmt.Println("c. Mul")
	fmt.Println("d. Div")

	fmt.Print("\nEnter Your Choice :\t")
	fmt.Scanf("%c", &ch)

	var a, b float64

	switch ch {
	case 'a':
		{
			fmt.Print("\nEnter Two Number:\t")
			fmt.Scan(&a, &b)
			fmt.Print("\nAns :\t", a+b)
		}
	case 'b':
		{
			fmt.Print("\nEnter Two Number:\t")
			fmt.Scan(&a, &b)
			fmt.Print("\nAns :\t", a-b)
		}
	case 'c':
		{
			fmt.Print("\nEnter Two Number:\t")
			fmt.Scan(&a, &b)
			fmt.Print("\nAns :\t", a*b)
		}
	case 'd':
		{
			fmt.Print("\nEnter Two Number:\t")
			fmt.Scan(&a, &b)
			if b != 0 {
				fmt.Print("\nAns :\t", a/b)
			} else {
				fmt.Print("\nB Must Be Non Zero.")
			}
		}
	default:
		{
			fmt.Println("\nIt's Seems You Entered A Wrong Choice!!!.")
		}
	}
	fmt.Print("\n\nWant To Do Again (y/n): \t\t")  
	fmt.Scan(&ch)  // Whats The Problem Here
}

}


( Kvaz1r) #2

It’s expected. From the documentation:

The one exception: the verb %c always scans the next rune in the input, even if it is a space (or tab etc.) or newline.

So or replace rune with string/integer or read rune by rune until the very end(in any sense).


(system) closed #3

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.