Can someone please tell me what im doing wrong in this program..im gettin a compiler error saying:

C:\Dev-Cpp\projects\proto.cpp In function `int main(int, char**)':
46 C:\Dev-Cpp\projects\proto.cpp a function-definition is not allowed here before '{' token
46 C:\Dev-Cpp\projects\proto.cpp expected `,' or `;' before '{' token
C:\Dev-Cpp\projects\Makefile.win [Build Error] [proto.o] Error 1

The error is occuring at the function definition..

The code is:

#include <stdio.h>
#include <cstdlib>
#include <iostream>

using namespace std;

void print(int x); /* Function prototype */

int main(int argc, char *argv[])
          {
       int i, j, m = 0, IP[64],P[64] = {0,1,0,0,0,1,0,0,
                                        0,1,1,1,0,1,0,0,
                                        0,1,0,1,0,1,0,0,
                                        0,1,1,1,0,1,0,0,
                                        0,1,0,0,0,1,0,0,
                                        0,1,1,1,0,1,0,0,
                                        0,1,0,0,0,1,0,0,
                                        0,1,1,1,0,1,0,1,};

  void printtable(int x);            /* function call */

/*INITIAL PERMUTATION*/
  int k = 57; 
   for (i = 0; i<4; ++i) { 
    for (j=k; j>=1; j=j-8){ 
     IP[m]=P[j];
     printf("%2d", IP[m]);
   ++m; 
   }
 k=k+2;
}

printf("\n");

  k = k-9;
   for (i = 0; i<4; ++i) { 
    for (j=k; j>=0; j=j-8){ 
     IP[m]=P[j];
    printf("%2d", IP[m]);
   ++m; 
  }
 k=k+2;
}


  void print(int x)       /* Function definition */
     {
    int x;
     for (x=0;x<10;x++){
       printf("%d",x);
      printf("n");
    }
  }  
}

Thanks

PS Its a DEV C++ COMPILER..

Recommended Answers

All 10 Replies

I would check the number of } braces you have in your function definition.

Also calling a function print is probably a bad idea, considering you already have fprintf, printf, sprintf, vprintf, ... defined in the standard library.

Why these lines?

#include <iostream>

using namespace std;

Why these lines?

#include <iostream>
using namespace std;

because he thinks this is the C++ forum.

#include <iostream>
using namespace std;

gerard4143
Its a C++ compiler, #include<iostream> is automatically in the code..Could take it out but it works either way.. using namespace std; is a typo..it wasnt in the code when i compiled it.. think i have the rite amount of braces for the function definition: 2 open and 2 closed, then the final brace to close main..also it doesnt matter what name i give function im still getin this error

remove <iostream>
remove <cstdlib> ... replace with <stdlib.h>
remove "using namespace"

calling a function "print" is bad practice.

void print(int x)
should not also have a local redeclaration of "int x" choose either one or the other. what is this doing, anyhow? printing 1-10? why do you need a dedicated function to do that?


good lord this is a mess.

where does your main() routine even end? where do your function definitions begin? are your functions "printtable" and "print" the same functions?

clean up this mess of code and repost it using proper indentations and code tags, so we can see what the heck is going on.

this is unreadable.

Move the brace at 54 to 45 and the code resolves into a main function and a print function.

Call the print function from the main to execute it. (print)

I'm not convinced that's correct. i'm not even convinced the indentions that have appeared since Walt added the code-tags are correct. they're pretty much a mess too.

i'm half suspecting the print() and printtable() are really the same function that was just misnamed in one spot.

we won't really know anything until the OP returns to clean up his code.

Thanks for replies guys..

Jephthah
sorry for initial code layout ive cleared it up..First time using site..Yes print and printtable were same function sorry for that typo..ive reposted code using the latter..The reason im using a dedicated function just for this is because Im starting off and do not know how to call functions => thought id just keep it simple and see if i could actually get the method right..I did not know that once you listed the parameter types in the definition that u didnt have to declare them locally in the function, thanks for clearing that up with me.

Thomas
Thanks..I thought the main() braces had to enclose everything..Moved them like you said and its compiling fine now..Problem is though im not getting any output from the printtable function.. im calling it within main but the only output im getting is from the Initial Permutation loop..Can anyone help me out here please??

Many thanks again

#include <stdio.h>
#include <stdlib.h>

void printtable(int x);             /* Function prototype */

int main(void)
          {
       
       void printtable();                    /* Function call */

       int i, j, m = 0, IP[64],P[64] = {0,1,0,0,0,1,0,0,
                                        0,1,1,1,0,1,0,0,
                                        0,1,0,1,0,1,0,0,
                                        0,1,1,1,0,1,0,0,
                                        0,1,0,0,0,1,0,0,
                                        0,1,1,1,0,1,0,0,
                                        0,1,0,0,0,1,0,0,
                                        0,1,1,1,0,1,0,1,};


/*INITIAL PERMUTATION*/
  int k = 57; 
   for (i = 0; i<4; ++i) { 
    for (j=k; j>=1; j=j-8){ 
     IP[m]=P[j];
     printf("%2d", IP[m]);
     ++m; 
    }
  k=k+2;
}

printf("\n");

  k = k-9;
   for (i = 0; i<4; ++i) { 
    for (j=k; j>=0; j=j-8){ 
     IP[m]=P[j];
     printf("%2d", IP[m]);
     ++m; 
    }
 k=k+2;
   }
}                                          /* End of main() */


  void printtable(int x)          /* Function definition */
     {
      for (x=0; x<10; x++) {
        printf("%d",x);
        printf("n");
    }
  }

replace line 9 with

printtable(10);

Note 10 has no significance here since the value of x is ignored.

when you declare a prototype, like in line 4, you declare the function type, function name, and argument types, like you did: void printtable(int x); , this must match the definition of the function, which you have down on line 46.

but when you *call* the function in the program, like at line 9, do not include the function type or argument types, so you just call it like so: printtable(10); .... where the number "10" (for example) could be any "int" value and will be passed to the function.

when you get to the function definition, starting at line 46:

void printtable(int x)          /* Function definition */
{
    for (x=0; x<10; x++) 
    {
        printf("%d",x);
        printf("n");
    }
}

you see that whatever value passed in from the function call (line 9) will come into this function via the argument as "int x"... the purpose of this would be to have a value that could then be used for whatever purpose you intended within the function.

HOWEVER, what you are doing is not really correct, or at least is not what is intended. once you get to the function, you reassign "x=0" in your FOR loop, thereby losing whatever value was passed in.

it seems to me that you really intend to have "x" be the upper limit of your for loop, so that it counts out "x" numbers. change your printtable function to something like this:

void printtable(int x)          /* Function definition */
{
    int i;                  // a local index, so you don't destroy 'x'

    for (i=0; i<x; i++)     // counts from 0 to (x-1)
    {
        printf("%d ", i);   // added space to print between incrementing index numbers
        printf("\n");       // change 'n' to '\n' for newline
    }
}

now when you call from line #9, you can pass any argument and that will be the number used by the function. otherwise, if you're just hardcoding x=10 in the function, you might as well delete the "int x" from the function argument and replace it with "void", and then there would really no point in having the function for such a trivial operation.

.

Thanks a million guys, have got it working now..Yous have been most helpfull, cheers

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.