Parse Error Help

This is a discussion on Parse Error Help within the C Programming forums, part of the General Programming Boards category; 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: ...

  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
    21,452
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,682
    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

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