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;
}
> 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>
<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.
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.
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 :(
<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.
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.
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
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 :(
> 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>
>> 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.
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>
--
Ben.
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.
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.
--
Keith Thompson (The_Other_Keith) ks...@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
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.
> 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.
--
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.
--
Kenneth Brody
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.)