Results 1 to 3 of 3

Thread: Simple C program help =)

  1. #1
    Join Date
    Sep 2009
    Location
    Georgia, USA
    Beans
    66
    Distro
    Ubuntu Karmic Koala (testing)

    Simple C program help =)

    third post with this program XD and its not getting far... anyway I keep getting the error "‘else’ without a previous ‘if’"while compileing. I understand what this means but i dont see why it does not see what im doing.. i have 3 modules, the middle on breaks off into another module but even though there is something after the second one i don't know why the third one is not recognised.. any help?
    PS. i tried to work on my "Style" hope its readable.

    code
    Code:
    #include <stdio.h>
    
    int main()
    {        /*starting main function.*/
    char zombie;
    
    printf ( "a man approaches you; His face hidden in the shadows\n" );
    printf ( "'are you  a zombie?'\n (y/n)\n" );        /*asks for a yes or no answer*/
    scanf ( "%c", &zombie );        /*waits for input*/
    if (zombie =='y' ){        /*if you said yes*/
        printf( "you have obviously already lost\n" );
        }
    else if (zombie == 'n'){        /*if you answered no it will start the next "level" of the game.*/
        printf ( "The man comes into the light; His face rotten and maggot-filled, He mumbles as he lumbers to you 'You ought to run than'\n" );
    }
    char ch;
    while((ch = getc(stdin)) != EOF && ch != '\n'){        /*these two lines are supposed to clear the %c char? */
        continue;
    }                                        /* so that I can reuse it for the "run" command (i did not write this)*/
        char run;
        scanf ("%c", &run );
        if (run == 'r' ){
            printf ( "you run just fast enough to outrun him and make it into a safe room\n" );
        }
        
        else {
            printf ( "You are to slow and got eaten.\n" );
        }        
    
    else {        /*This is what happens if you did not press Y or N in the beginning.*/
        printf ( "you fail... I SAID YES OR NO!!\n" );
    }
    
        
    }        /*ending main function*/

  2. #2
    Join Date
    Apr 2009
    Location
    Germany
    Beans
    2,134
    Distro
    Ubuntu Development Release

    Re: Simple C program help =)

    this else:
    else { /*This is what happens if you did not press Y or N in the beginning.*/
    printf ( "you fail... I SAID YES OR NO!!\n" );
    }
    has no matching if.
    every if clause is closed before this is reached
    I'm guessing the else { should be after the else if (zombie == 'n'){?

    an editor with bracket matching and proper indentation is very useful for these kind of things
    Last edited by MadCow108; October 25th, 2009 at 11:24 PM.

  3. #3
    Join Date
    Sep 2009
    Location
    Georgia, USA
    Beans
    66
    Distro
    Ubuntu Karmic Koala (testing)

    Re: Simple C program help =)

    i actually figured it out. I took out the ending bracket of the "(if zombie == 'n') and moved it closer to the bottom thus causing the things i wanted to be inside this function.

    what would you suggest? im using gedit and that has bracket matching and it will CONTINUE an indent.. if im indented and press inter it will stay that much indented. but this is not auto indention.

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •