Thread: Parse Error Help

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    9

    Parse Error Help

    I'm getting the following error in my code and can't figure out why:

    assign4.c: In function `int main()':
    assign4.c:18: error: parse error before `{' token
    assign4.c:50: error: parse error before `}' token

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int size;
    int seed;
    int row;
    int col;
    int cnum;
    int rnum;
    int space;
    int x;
    int y;
    
    
    int main()
    {
            int loop()
            {
                    printf("Enter size and seed:  ");
                    int n = scanf("%i $i", &size, &seed);
                    if(n == 2)
                    {
                            for(row = 1; row == size; row++)
                            {
                                    printf("%i ", seed);
                                    for(col = 1; col == size; col++)
                                    {
                                            cnum = seed;
                                            if((x = cnum + col) <= 9)
                                            {
                                                    printf("%i ", x);
                                            }
                                            else
                                            {
                                                    y = x - 10;
                                                    printf("%i ", y);
                                            }
                                    }
                                    printf("\n");
                                    for(space = row; space == 0; space--)
                                    {
                                            printf(" ");
                                    }
                            }
                    }
                    else
                    {
                            printf("Size and seed must both be between 1 and 9.");
                            loop()
                    }
            }
            return EXIT_SUCCESS;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You should define your loop() function outside of the main() function and then call it from within the main() function. You also should use a more descriptive name than "loop". Oh, and turn those global variables into local variables.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    9
    Thanks. I just got rid of the loop() and just called the main. There was no reason to add that complication.

  4. #4
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Quote Originally Posted by frterwil View Post
    Thanks. I just got rid of the loop() and just called the main. There was no reason to add that complication.


    Oh well, the neat of doing this is by calling the loop from main and not calling the main recursively. Look bellow:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    int loop( void );
    int main()
    {
        loop();
        return EXIT_SUCCESS;
    }
    int loop( void )
    {
        int size, seed, row, col;
        int cnum, rnum, space, x, y;
        printf("Enter size and seed:  ");
        int n = scanf("%i %i", &size, &seed);
                    
        if(n == 2)
        {
             for(row = 1; row == size; row++)
             {
                 printf("%i", seed);
                 for(col = 1; col == size; col++)
                 {
                      cnum = seed;
                      if((x = cnum + col) <= 9)
                          printf("%i ", x);
                      else
                      {
                          y = x - 10;
                          printf("%i ", y);
                      }
                 }
                 printf("\n");
                 for(space = row; space == 0; space--)
                     printf(" ");
             }
        }
        else
        {
           printf("Size and seed must both be between 1 and 9.");
           loop();
        }
    }
    


    I am not very sure about the recursive function here. That is gonna be in a infinite loop unless you put some condition some where in the function so that it could break and return to main.

    Hope that help on how to follow it up.

    -ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Recursion to make a loop is a bad way to do loops UNLESS the problem itself is a recursive type problem (e.g factorial, binary tree search, etc).

    This bit of code:
    Code:
             for(row = 1; row == size; row++)
             {
                 printf("%i", seed);
                 for(col = 1; col == size; col++)
    will most likely fail to do what you want it to do - unless you wish it to ONLY run the loop when size == 1.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Interpreter.c
    By moussa in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 05:59 PM
  2. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 05:45 AM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM