![]() |
Compiling error: expected identifier or ‘(’ before ‘{’ token
Hi I've been writing a piece of code as part of university course and
I am getting an error and I can't figure out how to fix it. The code is meant to be a reverse polish notation calculator, the user inputs digits and operators one at a time and the program either pushes the numbers on to a stack or pops them off when it receives an operator, performs the calculation, then pushes the result back on to the stack. Finally when the user inputs a equals sign the program displays the results, this is how it's meant to work anyway. When I try to compile i get this error: prog5.c:10: error: expected identifier or ‘(’ before ‘{’ token BTW the simpleio.h is a library of input mechanisms given to us by our university, they include the getInt() & getChar functions, supposedly there to make our lives easier, anyway, the code: #include <stdio.h> #include <stdlib.h> #include "../simpleio.h" typedef struct CalcParam { int item; struct calcParam * next; } calcParam; int main(); { char item; int temp1, temp2, temp3, answer; calcParam * top; calcParam * cpp; printf("Enter a reverse polish notation string, character by character: \n"); printf("Finish with '='\n"); printf("Next: "); item=getChar(); while(item!='=') { if (!(item>='0' && item<='9' || item=='+' || item=='-' || item=='*' || item=='/' ) //ccheck if input is valid { printf("Not a valid input, valid input: digits 0-9, +, -, *, /"); exit(1) } if (item>='0' && item<='9') //check if user input is a number { if(!(cpp = (calcParam *) malloc(sizeof(calcParam)); //check if memory can be allocated { printf("Cannot allocate memory\n"); exit(1); } atoi(&item); //convert input char to int ready to push on to stack cpp->item=item; //push number on to stack cpp->next = top; top = cpp; printf("Next: "); item=getChar(); } if (item=='+' || item=='-' || item=='*' || item=='/') // check if user input is an operator { temp1=top->item; //pop first item and put it in temp1 cpp = top; top = top->next; free(ccp); temp2=top->item; //pop second item and put it in temp2 cpp = top; top = top->next; free(ccp); temp3 = temp2 item temp1; //perform arithmetic and store answer in temp3 if(!(cpp = (calcParam *) malloc(sizeof(calcParam)); // check if memory can be allocated { printf("Cannot allocate memory\n"); exit(1); } cpp->item=temp3; //push temp3 on to stack cpp->next = top; top = cpp; printf("Next: "); item=getChar(); } } answer=top->item; //pop answer cpp = top; top = top->next; free(ccp); printf("Answer: ", answer); return 0; } |
Re: Compiling error: expected identifier or ‘(’ before ‘{’ token
On Mar 10, 7:51*am, aydinh <mr.ay...@gmail.com> wrote:
> int main(); > { Here's your problem. Tom |
Re: Compiling error: expected identifier or ‘(’ before ‘{’ token
On 10 Mar, 12:51, aydinh <mr.ay...@gmail.com> wrote:
> Hi I've been writing a piece of code as part of university course and > I am getting an error and I can't figure out how to fix it. <snip> > When I try to compile i get this error: > prog5.c:10: error: expected identifier or ‘(’ before ‘{’ token it would be nice if you indicated which was line 10. I know we can count but the posting process can mess things up. And suppose it was big number line 199, do I have to count that many lines to help you? > BTW the simpleio.h is a library of input mechanisms given to us by our > university, they include the getInt() & getChar functions, supposedly > there to make our lives easier, anyway, the code: > > #include <stdio.h> > #include <stdlib.h> > #include "../simpleio.h" > > typedef struct CalcParam { *int item; > * * * * * * * * * * * * * * struct calcParam * next; I think you meant "struct CalcParam". Is this your actual code? > * * * * * * * * * * * * * * } calcParam; > > int main(); > { > * * * * char item; I can find your error by inspection but you should learn to do this yourself. Which line was the error reported on? What came just before the { ? Ignore spaces, tabs and newlines. Now go and look in your textbook or course notes. How is a function defined? <snip> |
Re: Compiling error: expected identifier or ‘(’ before ‘{’ token
On 10 Mar, 12:51, aydinh <mr.ay...@gmail.com> wrote:
<snip> > The code is meant to be a reverse polish notation calculator, the user > inputs digits and operators one at a time and the program either > pushes the numbers on to a stack or pops them off when it receives an > operator, performs the calculation, then pushes the result back on to > the stack. > > Finally when the user inputs a equals sign the program displays the > results, this is how it's meant to work anyway. > > When I try to compile i get this error: > prog5.c:10: error: expected identifier or ‘(’ before ‘{’ token I dealt witht he error in another post > BTW the simpleio.h is a library of input mechanisms given to us by our > university, they include the getInt() & getChar functions, supposedly > there to make our lives easier, anyway, the code: > > #include <stdio.h> > #include <stdlib.h> > #include "../simpleio.h" > > typedef struct CalcParam { *int item; > * * * * * * * * * * * * * * struct calcParam * next; > * * * * * * * * * * * * * * } calcParam; > > int main(); > { > * * * * char item; > * * * * int temp1, temp2, temp3, answer; variables called temp1, temp2 and temp3 are usually a bad sign. Have you done arrays yet? > * * * * calcParam * top; > * * * * calcParam * cpp; > > * * * * printf("Enter a reverse polish notation string, character by > character: \n"); > * * * * printf("Finish with '='\n"); > * * * * printf("Next: "); item=getChar(); > > * * * * while(item!='=') did you use up your allocation of spaces? I'd code this as while (item != '=') > * * * * { > * * * * * * * * if (!(item>='0' && item<='9' || item=='+' || item=='-' || item=='*' > || item=='/' ) *//ccheck if input is valid have you be told about isdigit()? I'd worry about the precedence of && and ||. I * think* the above is correct but I'd check. > * * * * * * * * { > * * * * * * * * * * * * printf("Not a valid input, valid input: digits 0-9, +, -, *, /"); > * * * * * * * * * * * * exit(1) technically 1 (one) is a not a portable value to pass to exit(). Though it's going to work most places. > * * * * * * * * } > > * * * * * * * * if (item>='0' && item<='9') * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //check if user input is a > number // comments don't work on older compilers and get messed up by posting s/w (as here). > * * * * * * * * { > * * * * * * * * * * * * if(!(cpp = (calcParam *) malloc(sizeof(calcParam)); * * * * * * * * * * * * * * //check if > memory can be allocated is malloc() really necessary? Your stack is a list of calcParams? You chekced the return value of malloc()- good! The cast is unnecessary in C. > * * * * * * * * * * * * { > * * * * * * * * * * * * * * * * printf("Cannot allocate memory\n"); > * * * * * * * * * * * * * * * * exit(1); > * * * * * * * * * * * * } > * * * * * * * * * * * * atoi(&item); * * you ignore the return value of atoi(). atoi() expects a string you can't give it the address of a char. Review your pointer stuff. atoi() has no error checking but I suppose that's ok for a learner. * > * * * * * * * * * * * * cpp->item=item; I've removed some of your comments as the layout had gone *really* strange > * * * * * * * * * * * * cpp->next = top; > * * * * * * * * * * * * top = cpp; > * * * * * * * * * * * * printf("Next: "); item=getChar(); > * * * * * * * * } > * * * * * * * * if (item=='+' || item=='-' || item=='*' || item=='/') * * > * * * * * * { > * * * * * * * * * * * * temp1=top->item; * where was top initialised (I may have missed it). Have you done functions yet? push and pop probably should be functions. * * * > > * * * * * * * * * * * * cpp = top; > * * * * * * * * * * * * top = top->next; > * * * * * * * * * * * * free(ccp); > * * * * * * * * * * * * temp2=top->item; * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //pop second item and put it in > temp2 I hate comments like this! We *know* you are storing it in temp2. > * * * * * * * * * * * * cpp = top; > * * * * * * * * * * * * top = top->next; > * * * * * * * * * * * * free(ccp); > * * * * * * * * * * * * temp3 = temp2 item temp1; * * * * * * * * * * * * * * * * * * * * * * * * * * * //perform arithmetic and > store answer in temp3 > > * * * * * * * * * * * * if(!(cpp = (calcParam *) malloc(sizeof(calcParam)); * * * * * * * * * * * * * * // > check if memory can be allocated > * * * * * * * * * * * * { > * * * * * * * * * * * * * * * * printf("Cannot allocate memory\n"); > * * * * * * * * * * * * * * * * exit(1); > * * * * * * * * * * * * } > * * * * * * * * * * * * cpp->item=temp3; * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //push temp3 on to stack > * * * * * * * * * * * * cpp->next = top; > * * * * * * * * * * * * top = cpp; > * * * * * * * * * * * * printf("Next: "); item=getChar(); > * * * * * * * * } > * * * * } > * * * * answer=top->item; * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//pop answer > * * * * cpp = top; > * * * * top = top->next; > * * * * free(ccp); > * * * * printf("Answer: ", answer); > * * * * return 0; > > > > } |
Re: Compiling error: expected identifier or ‘(’ before ‘{’ token
On Mar 10, 1:04*pm, Nick Keighley <nick_keighley_nos...@hotmail.com>
wrote: > On 10 Mar, 12:51, aydinh <mr.ay...@gmail.com> wrote: > > > Hi I've been writing a piece of code as part of university course and > > I am getting an error and I can't figure out how to fix it. > > <snip> > > > When I try to compile i get this error: > > prog5.c:10: error: expected identifier or ‘(’ before ‘{’ token > > it would be nice if you indicated which was line 10. I know we can > count but the posting process can mess things up. And suppose it was > big number line 199, do I have to count that many lines to help you? > > > BTW the simpleio.h is a library of input mechanisms given to us by our > > university, they include the getInt() & getChar functions, supposedly > > there to make our lives easier, anyway, the code: > > > #include <stdio.h> > > #include <stdlib.h> > > #include "../simpleio.h" > > > typedef struct CalcParam { *int item; > > * * * * * * * * * * * * * * struct calcParam * next; > > I think you meant "struct CalcParam". Is this your actual code? > > > * * * * * * * * * * * * * * } calcParam; > > > int main(); > > { > > * * * * char item; > > I can find your error by inspection but you should learn to do this > yourself. Which line was the error reported on? What came just before > the { ? Ignore spaces, tabs and newlines. Now go and look in your > textbook or course notes. How is a function defined? > > <snip> line 10 is the line after int main, which as pointed out above I ridiculously left in a semi-colon without noticing :(, the definition of struct calcParam is right, and yes it is my own code. |
Re: Compiling error: expected identifier or ‘(’ before ‘{’ token
Thanks for all the comments, the code works now and I am going to read
through all your suggestions, I have learnt functions and arrays, but this exercise is under the lab worksheet for malloc, so I guess it's best I use malloc. I do understand the precedence of || and &&, but haven't come across isdigit, will look in to it. exit(1) is just what I have been shown by my lecturer, I know no other way :( I did forget to initialise top, I have amended it and it now looks like this: calcParam * top = NULL; Sorry about the annoying comments :( |
Re: Compiling error: expected identifier or ‘(’ before ‘{’ token
On 10 Mar, 13:21, aydinh <mr.ay...@gmail.com> wrote:
> On Mar 10, 1:04*pm, Nick Keighley <nick_keighley_nos...@hotmail.com> > > On 10 Mar, 12:51, aydinh <mr.ay...@gmail.com> wrote: <snip> > > > typedef struct CalcParam { *int item; > > > * * * * * * * * * * * * * * struct calcParam * next; > > > I think you meant "struct CalcParam". Is this your actual code? > > > > * * * * * * * * * * * * * * } calcParam; <snip> > line 10 is the line after int main, which as pointed out above I > ridiculously left in a semi-colon without noticing :(, it happens! But the compiler tries its best to help! > the definition > of struct calcParam is right, and yes it is my own code. must be me. I'd expect typedef struct CalcParam { int item; struct CalcParam * next; } calcParam; I'm surprised it can "see" calcParam when next is defined. |
Re: Compiling error: expected identifier or ‘(’ before ‘{’ token
> I'm surprised it can "see" calcParam when next is defined.
I know what you mean, but this is how I've been shown to do it, and both my reference books say to do it this way. |
Re:Compiling error: expected identifier or ‘(’ before ‘{’ token
On 2010-03-10, aydinh <mr.aydin@gmail.com> wrote:
> Hi I've been writing a piece of code as part of university course and > I am getting an error and I can't figure out how to fix it. > > The code is meant to be a reverse polish notation calculator, the user > inputs digits and operators one at a time and the program either > pushes the numbers on to a stack or pops them off when it receives an > operator, performs the calculation, then pushes the result back on to > the stack. > > Finally when the user inputs a equals sign the program displays the > results, this is how it's meant to work anyway. > > When I try to compile i get this error: > prog5.c:10: error: expected identifier or ?(? before ?{? token > > BTW the simpleio.h is a library of input mechanisms given to us by our > university, they include the getInt() & getChar functions, supposedly > there to make our lives easier, anyway, the code: > > #include <stdio.h> > #include <stdlib.h> > #include "../simpleio.h" > > typedef struct CalcParam { int item; > struct calcParam * next; > } calcParam; > > int main(); Compilation fails here, as your compiler as told you. > { > char item; Don't use tabs. Your lines are too long even without them. My newsreader will complain when I try to post this because I've quoted /your/ lines. Ergo, you need a smarter newsreader. Try slrn. > int temp1, temp2, temp3, answer; > calcParam * top; > calcParam * cpp; > > printf("Enter a reverse polish notation string, character by > character: \n"); Character by character? Surely you can do better than that. > printf("Finish with '='\n"); > printf("Next: "); item=getChar(); > > while(item!='=') What if item is EOF? > { > if (!(item>='0' && item<='9' || item=='+' || item=='-' || item=='*' >|| item=='/' ) //ccheck if input is valid Please stop using // comments. I can't even count all the syntax errors you've caused by them wrapping around all over the place. > { > printf("Not a valid input, valid input: digits 0-9, +, -, *, /"); > exit(1) Compilation will fail here. Plus 1 is not a valid return value on all systems. stdlib.h has a constant EXIT_FAILURE you can use for that purpose. > } > > if (item>='0' && item<='9') //check if user input is a > number See ctype.h and isdigit(). > { > if(!(cpp = (calcParam *) malloc(sizeof(calcParam)); //check if > memory can be allocated This is a terrible use of malloc(). Try: cpp = malloc(sizeof *cpp) > { > printf("Cannot allocate memory\n"); > exit(1); > } > atoi(&item); //convert input char to int ready to push on > to stack What do you expect the to do? And how? > cpp->item=item; //push number on to stack > cpp->next = top; > top = cpp; > printf("Next: "); item=getChar(); > } > if (item=='+' || item=='-' || item=='*' || item=='/') // > check if user input is an operator > { > temp1=top->item; //pop first item and put it in > temp1 > cpp = top; > top = top->next; > free(ccp); > temp2=top->item; //pop second item and put it in > temp2 > cpp = top; > top = top->next; > free(ccp); > temp3 = temp2 item temp1; //perform arithmetic and > store answer in temp3 What do you expect this to do? And how? > > if(!(cpp = (calcParam *) malloc(sizeof(calcParam)); // > check if memory can be allocated > { > printf("Cannot allocate memory\n"); > exit(1); > } > cpp->item=temp3; //push temp3 on to stack > cpp->next = top; > top = cpp; > printf("Next: "); item=getChar(); Why did you duplicate this line? > } > } > answer=top->item; //pop answer > cpp = top; > top = top->next; > free(ccp); This is pointless. If you're going to free anything why don't you free the entire list? > printf("Answer: ", answer); > return 0; > } -- Andrew Poelstra http://www.wpsoftware.net/andrew |
Re:Re: Compiling error: expected identifier or ‘(’ before ‘{’ token
On 2010-03-10, aydinh <mr.aydin@gmail.com> wrote:
>> I'm surprised it can "see" calcParam when next is defined. > > I know what you mean, but this is how I've been shown to do it, and > both my reference books say to do it this way. Throw them both out. They're garbage. -- Andrew Poelstra http://www.wpsoftware.net/andrew |
| All times are GMT. The time now is 10:46 AM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.