简体   繁体   English

程序打印两次相同的打印语句

[英]Program prints out the same print statement twice

My program is working fine and everything but I want it to be perfect. 我的程序运行正常,除了我希望它是完美的。 So I've stumbled up on this problem that when my function runs, it prints out the same printf() statement twice. 所以我偶然发现了这个问题,当我的函数运行时,它会打印两次相同的printf()语句。

Let me show you what I mean, this is how my function looks (skipping main/prototypes) 让我告诉你我的意思,这是我的功能看起来(跳过主/原型)

void decision2(struct CardDeck *deck) {
char choice;
int Ess;
printf("\n%s, Your have %d\n", deck->name2, deck->ValueOfSecondPlayer);
while (deck->ValueOfSecondPlayer <= 21)
{
    if (deck->ValueOfSecondPlayer == 21) {
        printf("You got 21, nice!\n");
        break;
    }
    if (deck->ValueOfSecondPlayer < 21) {
        printf("Would you like to hit or stand?\n");
        scanf("%c", &choice);
    }
    if (choice == 'H' || choice == 'h') {
        printf("You wish to hit; here's your card\n");
        Ess = printCards(deck);
        if (Ess == 11 && deck->ValueOfSecondPlayer > 10)
        {
            Ess = 1;
        }
        deck->ValueOfSecondPlayer += Ess;
        printf("Your total is now %d\n", deck->ValueOfSecondPlayer);
        if (deck->ValueOfSecondPlayer > 21) {
            printf("Sorry, you lose\n");
        }
    }
    if (choice == 'S' || choice == 's') {
        printf("You wished to stay\n");
        break;
    }
}

} }

So the thing in my code thats weird is this part: 所以我的代码中的东西很奇怪就是这部分:

    if (deck->ValueOfSecondPlayer < 21) {
        printf("Would you like to hit or stand?\n");
        scanf("%c", &choice);
    }

The output of the program is becoming this: 该计划的输出正在变为:

    k, Your have 4
Would you like to hit or stand?
Would you like to hit or stand?
h
You wish to hit; here's your card
6 of Clubs
Your total is now 10
Would you like to hit or stand?
Would you like to hit or stand?
h
You wish to hit; here's your card
King of Diamonds
Your total is now 20
Would you like to hit or stand?
Would you like to hit or stand?
s
You wished to stay

As you see, the printf prints the statement twice and I can't figure out the program to be honest, so I hope someone do have a solution and an explanation why this is happening? 正如你所看到的,printf打印了两次声明,说实话我无法弄清楚程序,所以我希望有人能找到解决方案并解释为什么会发生这种情况?

The problem here is, with 这里的问题是,与

 scanf("%c", &choice);

it reads the previously stored newline (entered into the input buffer by pressing ENTER key after the first input) and performs one more iteration. 它读取先前存储的newline (在第一次输入后按ENTER键进入输入缓冲区)并再执行一次迭代。 You should write 你应该写

 scanf(" %c", &choice);  //note the space here
       ^^

to avoid the newline. 避免换行。

To elaborate, the leading space before the %c ignores any number of leading whitespace character(s) [FWIW, a newline is a whitespace character] and wait to get a non-whitespace input. 详细说明, %c之前的前导空格忽略任意数量的前导空白字符[FWIW, newline是空格字符]并等待获得非空白字符输入。

There has to be a space before %c in scanf() , to clear the previously entered newline \\n : scanf() %c之前必须有一个空格,以清除先前输入的换行符\\n

scanf(" %c", &choice);

Without the space, scanf() will scan the newline and jump to the beginning of the loop again (after skipping the remaining if statements), skipping the first if() , and print "Would you like to hit or stand?\\n" a second time. 没有空格, scanf()将扫描换行符并再次跳转到循环的开头(跳过剩余的if语句后),跳过第一个if() ,然后打印"Would you like to hit or stand?\\n"第二次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM