2

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;
}
mahendiran.b
  • 1,315
  • 7
  • 13
  • Next time onwards,posting the **exact** error message like `syntax error : missing ';' before 'type'` will be really helpful to us. – Spikatrix Oct 04 '14 at 05:53

3 Answers3

2

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.

Spikatrix
  • 20,225
  • 7
  • 37
  • 83
2

As mentioned in other answers, C89 does not support declaring variables other than at the start of the block. If you are using clang or gcc, you might want to add '-std=gnu99' to your CFLAGS. If using another compiler or an IDE, look for the language and change it to C99 or higher.

Jeremy Huddleston Sequoia
  • 22,938
  • 5
  • 78
  • 86
1

It seems that the compiler requires that all definitions of varaibles would be in the beginninh of block. Try to write

char card_name[3];
int val = 0;
puts("카드 이름을 입력하세요: ");

Also take into account that array card_name is not initialized.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335