使用Scanf + input + enter会导致从stdin中输入两次,如何清空缓冲区?

huangapple go评论86阅读模式
英文:

using Scanf + input + enter makes for double input from stdin, how to flush?

问题

我正在使用一个便携式的未压缩版本的Go!每当我尝试使用Scanf(以及相关函数)进行输入输出控制台实现时,插入运行时输入并通过回车键进行验证会导致程序的行为(它是一个循环)像我输入了两次或三次一样。显然(就像在C语言中一样),在调用读取函数后需要清除标准输入,但我找不到如何做到这一点。我似乎是唯一一个遇到这个愚蠢的基本问题的人(为什么?)

在这个无限循环的程序中,即使在我糟糕的刷新尝试之后,问题被问了三次并得到了回答:

package main
import "fmt"
import "time"
var globalBad, globalGood int
func Thread1() {
    var i int
    var t string
    for {
    fmt.Println("Please give I")
    fmt.Scanf("%d", &i)
    fmt.Println(t);
    flush();
    globalBad = i;
    //fmt.Println(i);
    time.Sleep(1000 * time.Millisecond);
    fmt.Println("Meet globalGood %f", globalGood )
    if i == 12 {return};
    }

}

func Endless(){
    var LocalBad int ;

for{
    if LocalBad != globalBad{
        LocalBad = globalBad;
        globalGood = globalBad*2;
        }
    time.Sleep(1000 * time.Millisecond);
}
}

func flush(){
    var i byte
    for i > 0{
    fmt.readByte(i, var j);
    }
    fmt.Println("Done");
}

func main() {
    globalBad = 0;
    go Thread1();
    Endless();
}
英文:

I'm using a portable unzipped version of Go! Whenever I try to do a input output console implementation using Scanf (but also related functions) inserting runtime input and validating with enter results in the program behaving (it's a loop) like I inputted twice or three times. Obviously (like in C) the stdin needs to be cleared after calling the reading-function, but I don't find how to do that. I seem to be the only one with this stupid basic problem (why?)

In this endless loop -program the question is asked and answered 3 times even after my poor flush attempt:

package main
import "fmt"
import "time"
var globalBad, globalGood int
func Thread1() {
	var i int
	var t string
	for {
	fmt.Println("Please give I")
	fmt.Scanf("%d", &i)
    fmt.Println(t);
    flush();
    globalBad = i;
    //fmt.Println(i);
    time.Sleep(1000 * time.Millisecond);
    fmt.Println("Meet globalGood %f", globalGood )
    if i == 12 {return};
	}

}

func Endless(){
	var LocalBad int ;

for{
	if LocalBad != globalBad{
		LocalBad = globalBad;
		globalGood = globalBad*2;
		}
	time.Sleep(1000 * time.Millisecond);
}
}

func flush(){
	var i byte
	for i > 0{
	fmt.readByte(i, var j);
	}
	fmt.Println("Done");
}

func main() {
	globalBad = 0;
	go Thread1();
	Endless();


	
}

答案1

得分: 1

我无法使你的代码编译通过。真正有问题的是fmt.readByte(i, var j)这一行。没有fmt.readByte函数,即使有也不会被导出。var j部分是无效的。

另外,你在协程之间共享内存,但没有以任何方式进行同步(例如使用通道)。这可能导致各种奇怪的问题发生。

请修复你的代码(最好使用gofmt格式化),然后我可以给你更具体的建议。

英文:

I can't get your code to compile. The really problematic line is the fmt.readByte(i, var j) line. There's no fmt.readByte function and if there were it wouldn't be exported. The var j part is just invalid.

Also, you're sharing memory between goroutines but not synchronizing in any way (like channels, for example). This can cause all kinds of strange things to happen.

Please fix your code (and preferably gofmt it) and I can give you more specific advice.

huangapple
  • 本文由 发表于 2012年8月20日 15:25:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/12033708.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定