Compiling error: expected identifier or ‘(’ before ‘{’ token

A

aydinh

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;
}
 
N

Nick Keighley

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.

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>
 
N

Nick Keighley

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.
     
                        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.
 
A

aydinh

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?




I think you meant "struct CalcParam". Is this your actual code?



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.
 
A

aydinh

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 :(
 
N

Nick Keighley

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.
 
A

aydinh

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.
 
A

Andrew Poelstra

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?
 
A

Andrew Poelstra

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.
 
A

Andrew Poelstra

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.

Actually, if you count each tab as one character you are under 72 chars
for each line. It was merely an illusion that you had more. My bad.
 
A

aydinh

Actually, if you count each tab as one character you are under 72 chars
for each line. It was merely an illusion that you had more. My bad.

I'm trying I only started programming 6 months ago for roughly 3 hours
a week :(
 
B

Ben Bacarisse

Nick Keighley said:
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.

I was at first, but I think the error is a subtle one.

Lets re-write without relying on case as the sole difference between
the names:

typedef struct stag { int i; struct tdef *next; } tdef;

The inner struct tdef defines a pointer to a new (incomplete) struct
type with a struct tag that just happens to be the same as the typedef
name that is being defined.

The OP should get an error when attempting to do anything with the
next pointer because it refers to an incomplete type. That is indeed
what happens, but to get that far I had to correct a whole host of
other syntax errors. I think the OP is just assuming it's correct
because there was not error at the point of definition.

To the OP: your definition of the struct is not correct and the
compiler will tell about it eventually.

<snip>
 
A

Andrew Poelstra

I'm trying I only started programming 6 months ago for roughly 3 hours
a week :(

In that case, I politely request that when posting code to Usenet,

1. Don't use the tab key on your keyboard. The most consistent
way that they display (assuming no servers on the chain delete
them) is IMHO an ugly one.
2. Don't use // comments. They wrap around and then your code
can't be copy-pasted and still compile.

Other than that you were netiquettically in the clear.
 
K

Keith Thompson

aydinh said:
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.

The point is that "calcParam" and "CalcParam" are two different
identifiers. If you spell them consistently:

typedef struct calcParam { int item;
struct calcParam * next;
} calcParam;

then you're ok.
 
K

Keith Thompson

Andrew Poelstra said:
In that case, I politely request that when posting code to Usenet,

1. Don't use the tab key on your keyboard. The most consistent
way that they display (assuming no servers on the chain delete
them) is IMHO an ugly one.

The problem isn't the tab key on the keyboard, it's the tab characters
in the source. (In my editor with my configuration, pressing
<tab> inserts spaces.)

But yes, it's a good idea to avoid tab characters in code posted here.
If you're on a Unix-like system, filtering the source through "expand"
(or perhaps "expand -t 4") is a good solution.
 
A

Andrew Poelstra

You appear to have missed his point... You are defining a struct called
"CalcParam", whose "next" member points to a struct named "calcParam". Note
the different spellings. Now, perhaps you have two different structs with
the almost-identical names, but your code doesn't show any.

I read his code a couple of times and it looked to me like he
was okay:

typedef struct _test {
int data1;
int data2;
struct _test *next;
} Test;

I added an underscore for clarity, but that was essentially
his definition, wasn't it? (I've lost the original message.)
 
K

Keith Thompson

Andrew Poelstra said:
I read his code a couple of times and it looked to me like he
was okay:

typedef struct _test {
int data1;
int data2;
struct _test *next;
} Test;

I added an underscore for clarity, but that was essentially
his definition, wasn't it? (I've lost the original message.)

No, he used two different names for the tag (and re-used one of those
names for the typedef).

Here's the declaration from the original post:

typedef struct CalcParam { int item;
struct calcParam * next;
} calcParam;

This is legal, but it creates a type "struct CalcParam" one of
whose members is a pointer to an incomplete type "struct calcParam".

(Note that using the same identifier for the struct tag and the
typedef is perfectly ok. Or you can use a different name if you
prefer, but you should pick some consistent convention.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top