Reputation: 43
Trying to solve a problem on kattis.com called "Bela", which requires some character comparison, but when i run my code the scanf() function does not get called the last couples time for the last couple iterations of the loop.
here is my code:
#include <stdio.h>
int main( void ) {`
char dom;
int n;
scanf("%d %c", &n, &dom);
n*=4;
int sum = 0;
for (int i = 0; i < n; i++) {
char num;
char suit;
scanf("%c%c", &num, &suit);
switch (num) {
case 'A':
sum += 11;
break;
case 'K':
sum += 4;
break;
case 'Q':
sum += 3;
break;
case 'J':
if (suit == dom) { sum +=20;}
else { sum += 2;}
break;
case 'T':
sum+=10;
break;
case '9':
if (suit == dom){sum+=14;}
break;
case '8':
break;
case '7':
break;
default:
continue;
}
}
printf("%d", sum);
return 0;
}
and when i run the with this test case program i get this:
:~$ ./a.out
^V
2 S
TH
9C
KS
QS
JS
TD3
AD
JH
:~$ TD
TD: command not found
:~$ AD
AD: command not found
:~$ JH
JH: command not found
why is the for loop not executing completely? Is there anyhting inherently wrong with my code that the switch case statement does not evaluate "TD", "AD", "JH"?
Upvotes: 1
Views: 396
Reputation: 2399
why is the for loop not executing completely?
Add printf("\nEnter:");
before second scanf check it. The loop execute fully but the scanf catches spaces.
So,
Add
while((ch=getchar()!='\n')&&ch!=EOF);
Before the second scanf or change second scanf to
scanf(" %c%c", &num, &suit);
^
This statements are ignore spaces(' ','\n',...)
Upvotes: 1