Моя программа работает нормально и все, но я хочу, чтобы она была идеальной. Поэтому я наткнулся на эту проблему, что, когда моя функция работает, она печатает один и тот же оператор printf()
дважды.Программа распечатывает тот же оператор печати дважды
Позвольте мне показать вам, что я имею в виду, это то, как моя функция выглядит (пропускающие основные/прототипы)
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;
}
}
}
Так что дело в моем коде вот странно, эта часть:
if (deck->ValueOfSecondPlayer < 21) {
printf("Would you like to hit or stand?\n");
scanf("%c", &choice);
}
выход программы становится это:
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
Как вы видите, printf печатает заявление дважды, и я не могу понять программу, если честно, поэтому я надеюсь, что у кого-то есть решение и объяснение, почему это происходит?
Вы используете только один символ с% c, но для его ввода вы нажимаете ENTER, что является новым символом. Либо игнорируйте \ r и \ n, либо, возможно, используйте fgets. – LSerni