4
I am doing a school job, doing a blackjack (21) on C. In a certain part of the program, I created a while (while the option is yes) to show the cards, the score and ask if the user wants to bet again (yes or no) at the end of the while. Only that the compiler terminates the program and does not read the scanf at the end of while. If anyone can tell me how to fix it, I’d be grateful, I have to deliver it today.
#include <stdio.h>
#include <stdlib.h>
#define max 21
int main() {
char naipe, nome[50], op;
char tipo[13] = {'A','2','3','4','5','6','7','8','9','10','J','Q','K'};
int valor, valorb, soma=0, somab=0;
int i,din=1000;
int aposta, taposta=0;
printf("Digite o seu nome:\n");
scanf("%s",&nome);
op='S';
do {
system("cls");
printf("Você tem %d unidades\n",din);
printf("Escolha sua aposta inicial, %s:\n",nome);
printf("Digite 1 para 10 unidades\n");
printf("Digite 2 para 20 unidades\n");
printf("Digite 3 para 50 unidades\n");
scanf("%d",&aposta);
} while (aposta<1 || aposta>3);
switch (aposta) {
case 1:
taposta+=10;
break;
case 2:
taposta+=20;
break;
case 3:
taposta+=50;
break;
}
while ((op=='s') || (op=='S')) {
srand(time(NULL));
i=rand()%12+1;
if ((i==11) || (i==12) || (i==13))
valor=10;
else if (i==1)
valor=1;
else
valor=i;
naipe=rand()%3+3;
soma=(soma+valor);
printf("_________\n");
printf("|%c |\n",naipe);
printf("| |\n");
printf("| %c |\n",tipo[i]);
printf("| |\n");
printf("| %c|\n",naipe);
printf("\---------\n");
i=rand()%12+1;
if ((i==11) || (i==12) || (i==13))
valorb=10;
else if (i==1)
valorb=1;
else
valorb=(i+1);
naipe=rand()%3+3;
somab=(somab+valorb);
printf("_________\n");
printf("|%c |\n",naipe);
printf("| |\n");
printf("| %c |\n",tipo[i]);
printf("| |\n");
printf("| %c|\n",naipe);
printf("\---------\n");
printf("\n");
printf("Você marcou %d pontos e tem um total de %d pontos.\n",valor,soma);
printf("O computador marcou %d pontos e tem um total de %d pontos.\n",valorb,somab);
printf("\n");
printf("Deseja fazer a jogada? [S/N]\n");
scanf("%c",&op);
}
}
And where is the code?
– Maniero
Vacilo, I forgot. I already updated with the code
– user19954
Probably it is consuming the answer of the previous question in the scanf, so it never stops...
– Franchesco
This program doesn’t even compile, it has several errors.
– Maniero
It compiles yes, it is compiling here in codeblocks.
– user19954
Is that you let warnings off. For me Warning is wrong. Warning let compile but it will go wrong when it runs. So it makes no sense to accept that warnings exist. I only compile with their option to block compilation. Every professional programmer does this. Your code does not perform as you expect.
– Maniero
Here also does not compile. Warnig in C is same error.
– Jorge B.
No one here pointed out that using scanf is NOT considered safe and should not be used to receive user input. They gave the fish chewed but nobody here pointed out why the code does not work. You are not consuming stdin buffer. Another fflush can only be used in stdout, the use of fflush(stdin) is a violation. Disappointed with the pt OS.
– Vitim.us
@Vitim.us I mentioned this in my reply.
– Jorge B.