#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    4
    Rep Power
    0

    Program doesn't compile.... (newb question)


    // I'm using the pelles software //


    #include <stdio.h>

    int main(void);
    {
    int play;
    int quit;
    printf("Welcome to my game!. /n play /n quit");
    scanf("%d", play, quit);
    If ( play ){
    printf("Welcome to the game!");
    }
    else{
    printf("Goodbye!");
    }
    return 0;
    }
    I'm getting many errors that I don't really understand what they're telling me...can anyone tell me what I did wrong?
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,413
    Rep Power
    1871
    > int main(void);
    Why is there a ; here ?

    > scanf("%d", play, quit);
    Read the manual page for scanf again.
    - how many values are you reading?
    - how are you storing the values read in the variables you supply?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    4
    Rep Power
    0
    Comment accepted and I reread and changed my script accordingly, however now it tells me that my "else {" is an unrecognized statement...how so? it's exactly according to the syntax

    #include <stdio.h>
    #include <conio.h>

    int main(void)
    {
    char klaas;
    printf("Welcome to my game!");
    scanf("%c", &klaas);
    if (klaas == 'play')
    {
    printf("Welcome to the game!");
    }
    else if (klaas == 'quit')
    {
    printf("Goodbye!);
    }
    else {
    printf("Error!");
    printf("Please type either 'play' or 'quit' \n\n");
    }
    }
  6. #4
  7. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,413
    Rep Power
    1871
    > however now it tells me that my "else {" is an unrecognized statement...how so?
    The previous printf is missing a closing quote.

    Code:
     gcc -Wall baz.c
    baz.c: In function ‘main’:
    baz.c:9:14: warning: multi-character character constant [-Wmultichar]
    baz.c:13:19: warning: multi-character character constant [-Wmultichar]
    baz.c:15:8: warning: missing terminating " character [enabled by default]
    baz.c:15:1: error: missing terminating " character
    baz.c:16:1: error: expected expression before ‘}’ token
    baz.c:16:1: error: expected ‘;’ before ‘}’ token
    baz.c:21:1: warning: control reaches end of non-void function [-Wreturn-type]
    $
    Also, 'play' etc are the source of "warning: multi-character character constant"
    You can't type in 'play' and expect to read it with a single %c.
    You need
    - a char array
    - %s as the conversion format in scanf
    - strcmp to compare with "play" (note the use of double-quotes).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Location
    /dev/null
    Posts
    163
    Rep Power
    19
    1. You're expecting a string to be held in variable klaas, but you're defining it as a char which can hold only one character.

    2. klaas == 'play' --> This is not possible in C, as it is in scripting languages like shell/perl/python.

    3. When you say scanf("%c", &klaas), you're telling C to expect only one character. Even if you enter "play", only "p" will be referred by var "klaas".

    4. For string input/output, use the %s modifier
    Code:
    char klaas[10];
    printf("Welcome to my game!\n");
    scanf("%s", &klaas);
    5. For string comparisons, include the string library and use the function strcmp. Read more about the string library and let us know your findings.

IMN logo majestic logo threadwatch logo seochat tools logo