Search code examples
cputs

Confusion about error of program in C


When I compile this code I get an error "in front of int val, there isn't" ; how can I get rid of this error?

#include <stdio.h>
#include <stdlib.h>

int main() 
{
    char card_name[3];
    puts("카드 이름을 입력하세요: ");
    int val = 0;
    if(card_name[0]=='K') {
        val = 10;
    }
    else if (card_name[0] == 'Q') {
        val = 10;
    } 
    else if (card_name[0] == 'J') { 
        val = 10; 
    } 
    else if (card_name[0] == 'A') {
        val = 11;
    } 
    else 
    { 
        val = atoi(card_name); 
    }

    printf("카드값은 다음과 같습니다 : %i/n", val);
    return 0;
}

Solution

  • Declare all variables in the top of main just after { ,i.e, declare val before the first puts. It is because your compiler uses C89 which forbids mixed declarations and code. From C99 onwards , they can be declared (almost) anywhere.