Like Tree1Likes
  • 1 Post By whiteflags

Error "in function 'main' syntax error before 'int' Help Please

This is a discussion on Error "in function 'main' syntax error before 'int' Help Please within the C Programming forums, part of the General Programming Boards category; i have a dice game im making to start out in C and i keep getting this error Error "in ...

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    10

    Exclamation Error "in function 'main' syntax error before 'int' Help Please

    i have a dice game im making to start out in C and i keep getting this error
    Error "in function 'main' syntax error before 'int'
    Whats wrong with it?

    Code:
    #include <stdio.h>
    #include <conio.h>
    
    int RollDice(void);
    
    int main(void)
    {
        int numbGuess, userGuess, exitVal, diceRoll;
        exitVal = 0;
        numbGuess = 0;
        diceRoll = RollDice();  
          
        do{
                printf("Welcome To The Dice Game!\n");
                printf("You Have 5 Guesses To Pick The Right Number!\n");
                printf("Please Enter Your Guess For the Dice(E.x 1-6): ");
                scanf("%d", &userGuess);
                numbGuess++;
                
                if(userGuess == diceRoll)
                {
                     system("cls");
                     printf("You WIN!!!!\n");
                     printf("Number Of Guesses: %d", numbGuess);
                     exitVal = 1;
                }else 
                {
                           printf("Try Again. Press Enter To Put In A New Guess...");
                           getch();
                           system("cls");
                           numbGuess++;
                }
                
                if(numbGuess > 4)
                {
                     printf("To Many Guesses Game Over.");
                     
           
                }while(exitVal == 0);
        
        getch();
        return 0;
    }
    
    
    int RollDice(void)
    {
        srand(time(NULL));
        
        int Roll;
        
        Roll = 1 + rand() % 6;   
        
        return Roll;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You need as many open-squiggles
    Code:
    {
    as close squiggles
    Code:
    }
    in your code.

  3. #3
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,522
    Count you're curly braces and make sure you're not missing one. I think that's what's causing the syntax errors. By the way, when you're just starting out, it's a lot easier to pair braces right away and then type in the middle rather than try to remember to type them later. I still do it.

    For your edification, here is my compiler digest:
    Code:
    C:\Documents and Settings\Owner>gcc -c -Wall test.c
    test.c: In function 'main':
    test.c:22:18: warning: implicit declaration of function 'system'
    test.c:46:1: error: expected 'while' before 'int'
    test.c:55:1: error: expected declaration or statement at end of input
    And RollDice should not call srand(). srand() does not need to be called more than once, and I assume you will call RollDice many times, which in turn leads to not very random rolls.
    Last edited by whiteflags; 08-20-2011 at 06:38 PM. Reason: ninja's by tabstop!
    Salem likes this.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Yep... it's a bracing mismatch ... 5 opens and only 4 closes...

    Guessing from the includes and system calls... I'm betting that's Turbo C code, in which case the IDE does not support brace matching. A decent IDE editor would either pair the braces for you by automatically adding the close when you type an open or it would draw lines between them so you can see where your open ends... The latter is far more utilitarian since you can see, visually when something lands you in the wrong place.

  5. #5
    Registered User
    Join Date
    Aug 2011
    Posts
    10
    ok that was it guys. thank

  6. #6
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    To make it clear for the OP:
    Code:
     if(numbGuess > 4)
                {
                     printf("To Many Guesses Game Over.");
                     
                } //<-----add that    
       }while(exitVal == 0);
    Additionally, getch() is compiler specific and nonstandard. You should look into using a standard function such as getchar();


    Quote Originally Posted by whiteflags View Post
    And RollDice should not call srand(). srand() does not need to be called more than once, and I assume you will call RollDice many times, which in turn leads to not very random rolls.
    Actually, in this case the function is only called once so the OP's placement is ok. However, this is most likely by luck rather than careful thought so I do agree he should move the srand() call to the top of main.

    EDIT: OP posted while I was typing.....
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 04-18-2011, 11:39 AM
  2. strange "syntax error" problem :S
    By Akkernight in forum C++ Programming
    Replies: 23
    Last Post: 02-23-2009, 03:22 AM
  3. New Programmer main () syntax error
    By Bassglider in forum C Programming
    Replies: 4
    Last Post: 11-06-2007, 12:21 AM
  4. [HELP]Winsock Error "syntax error before ';' token"
    By C-isCool in forum C Programming
    Replies: 2
    Last Post: 07-05-2007, 07:32 PM
  5. First C program ? ? error: syntax error before "int"
    By Jasonx521 in forum C Programming
    Replies: 10
    Last Post: 10-02-2006, 09:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21