can anyone findout the error in this program

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jinnejeevansai
    New Member
    • Nov 2014
    • 48

    can anyone findout the error in this program

    error: expected '=', ',', ';', 'asm' or '__attribute__' before '.' token



    #include<stdio. h>
    #include<math.h >
    struct stack{
    int top;
    float d[100];
    }s;
    s.top==-1;
    void push(int a[],int j,int p)
    { int i;
    float s.d[s.top+1]=0;
    for(i=0;i<100;i ++)
    s.d[s.top+1]=(a[i]*pow(10,j-p))+s.d[s.top+1];
    s.top=s.top+1;
    }
    void pop(int a)
    {
    switch(a)
    {
    case '+':
    s.d[s.top-1]=s.d[s.top]+s.d[s.top-1];
    s.top=s.top-1;
    case '-':
    s.d[s.top-1]=s.d[s.top]-s.d[s.top-1];
    s.top=s.top-1;
    case '*':
    s.d[s.top-1]=s.d[s.top]*s.d[s.top-1];
    s.top=s.top-1;
    case '/':
    s.d[s.top-1]=s.d[s.top]/s.d[s.top-1];
    s.top=s.top-1;
    }
    }
    int main()
    {
    char a[100],n,i,j,p=0;
    scanf("%d",&n);
    for(i=0;i<n;i++ )
    {
    for(j=0;j<100;j ++)
    {
    scanf("%c",&a[j]);
    if(a[j]=='+'||'-'||'*'||'/')
    {
    pop(a[j]);
    }

    else if(a[j]==' ')
    {
    push(a,j,p);
    p=j;
    }
    else if(a[j]=='?')
    break;
    }
    }
    }
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    There are several errors in the code. Here are the first two:

    Code:
    struct stack{
    	int top;
    	float d[100];
    }s;
    s.top == -1;   <----ERROR
    void push(int a[], int j, int p)
    {
    	int i;
    	float s.d[s.top + 1] = 0;   <---ERROR
    	for (i = 0; i<100; i++)
    		s.d[s.top + 1] = (a[i] * pow(10, j - p)) + s.d[s.top + 1];
    	s.top = s.top + 1;
    }
    The first error is:
    Code:
     s.top == -1;
    First, this code is outside any function which is not allowed in C or C++. Second, it uses the equality operator (==) instead of the assignment operator (=).

    The second error is:
    Code:
    float s.d[s.top + 1] = 0;
    In this case, you define an array without specifying the number of elements. Remember, this is compile time so the number of elements must be known. s.top won't be evaluated until run time.

    To use this form of defining an array, you must call malloc(). This call would occur at run time and so s.top can also be evaluated to create an array of the correct size. An array created in this manner is called a dynamic array.

    There are other errors but this should get you started again.

    Comment

    Working...