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