Thread: errors when i first ran it

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    4

    errors when i first ran it

    prog1.c: In function ‘main’:
    prog1.c:181: error: case label in scope of identifier with variably modified type not containing enclosing switch statement
    prog1.c:253: error: case label in scope of identifier with variably modified type not containing enclosing switch statement
    prog1.c:317: error: expected declaration or statement at end of input
    prog1.c:317: error: expected declaration or statement at end of input


    I am attaching the c program for your reference. Thanks

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Jeez, 300+ lines and not a single indent!
    No wonder you can't find any errors for looking.

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #include<math.h>
    
    //powee function declaration 
    float powe(float x,float y)
    {
      if(y==0)
      return 1;
      else
      return(x*powe(x,y-1));
    }
    
    //bin to decimal conv function declaration
    bin2dec(int *dec,int k,int *bp)
    {
      *dec=0;
      int i;
      for(i=k-1;i>=0;i--)
      {*dec=*dec+(*(bp+i))*powe(2,k-1-i);}
      return(1);
    }   
    
    //main function 
    
    int main(int argc,char *argv[])
    {
      int r;
      char type1[] = "GAg";
      char type2[] = "GAp";
      char type3[] = "PAg";
      char type4[] = "Gshare";
      //assigning 'r' a value which is based on the type of scheme required here 
      if(strcmp(argv[2],type1)==0)
      r=1;
      if(strcmp(argv[2],type2)==0)
      r=2;
      if(strcmp(argv[2],type3)==0)
      r=3;
      if(strcmp(argv[2],type4)==0)
      r=4;
      switch(r)
      {
      case 1: //GAg scheme
        {
          int i,k,j;
          float hitrate;
          k=atoi(argv[4]);
          j=powe(k,2);
          int bht[k], pht[j];
          int branchaddr, targetaddr, tnt, dec; 
          int prediction; 
          //initialising the history bits to 0 and PHT entries to 01 as given
          for(i=0;i<k;i++)
          {bht[i]=0;}
          for(i=0;i<j;i++)
          {pht[i]=1;}
    
          //accessing the given text file
          FILE *fp1, *fp2;
          int total=0,wrong=0,correct=0; 
          fp1=fopen("history.txt", "r");
          fp2=fopen("output.txt", "w");
          while(fscanf(fp1,"%x %x %d",&branchaddr,&targetaddr,&tnt) != EOF) 
          {
            i=bin2dec(&dec,k,bht);  
            //doing a prediction here
            if(pht[dec]==0 || pht[dec]==1)
            {prediction==0;}
            if(pht[dec]==2 || pht[dec]==3)
            {prediction==1;}
            total=total+1;
            //checking if my prediction is ryt and inc the corresponding var
            if(prediction==tnt)
            {correct++;}
            if(prediction!=tnt)
            {wrong++;}
            //am updating the counters based on the tnt values given. 
            if(tnt==1)
            {
              if(pht[dec]<3)
              {pht[dec]++;}
            }
            if(tnt==0)
            {
              if(pht[dec]>0)
              {pht[dec]--;}
            }
            //updating the bht-usng ryt shift reg
            for(i=0; i<=k-1; i++)
            {bht[i]=bht[i+1];
              bht[k-1]=tnt;
            }
            i=bin2dec(&dec,k,bht);
            fprintf(fp2,"%d %d\t %d\t %d %d %d %d\n",tnt,prediction,dec,pht[0],pht[1],pht[2],pht[3] );
          }
          //printing the outputs
          printf("Total no of predictions made\n%d",total);
          printf("no of correct\n%d no of wrong \n%d",correct, wrong);
          hitrate=(correct/total)*100;
          printf("hitrate is\n%f", hitrate);
          break;
        }
      case 2://GAp scheme
        {
          int i,j,k,s,x,temp,n;
          int prediction;
          int total,correct,wrong;  
          float hitrate; 
          FILE *fp1,*fp2;
          int branchaddr,targetaddr,tnt,dec;
          k=atoi(argv[4]);
          s=atoi(argv[6]);
          j=powe(2,k);
          x=s/j; 
          int bht[k];
          int pht[j][x];
          //intialising all the entries of pht to 1
          for(i=0;i<j;i++)
          {
            for(n=0;n<x;n++)
            {
              pht[i][n]==0;
            }
          }
          //initialising all the entries of bht to 0 
          for(i=0;i<k;i++)
          {bht[i]=0;}
          //accessing the text file 
          fp1=fopen("history.txt","r");
          fp2=fopen("output.txt","w");
          while (fscanf(fp1,"%x %x %d",&branchaddr,&targetaddr,&tnt) != EOF)
          {
            if(k==2 && s==32)
            {temp=branchaddr&7;}
            if(k==2 && s==64)
            {temp=branchaddr&15;}
            if(k==2 && s==128)
            {temp=branchaddr&31;}
            if(k==4 && s==32)
            {temp=branchaddr&1;}
            if(k==4 && s==64)
            {temp=branchaddr&3;}
            if(k==4 && s==128)
            {temp=branchaddr&7;}
            i=bin2dec(&dec,k,bht);
            //doing a prediction here
            if(pht[dec][temp]==0 || pht[dec][temp]==1)
            {prediction==0;}
            if(pht[dec][temp]==2 || pht[dec][temp]==3)
            {prediction==1;}
            total=total+1;
            //checking if my prediction is ryt and inc the corresponding var
            if(prediction==tnt)
            {correct++;}
            if(prediction!=tnt)
            {wrong++;}
            //am updating the counters based on the tnt values given. 
            if(tnt==1)
            {
              if(pht[dec][temp]<3)
              {pht[dec][temp]++;}
            }
            if(tnt==0)
            {
              if(pht[dec][temp]>0)
              {pht[dec][temp]--;}
            }
            //updating the bht-usng ryt shift reg
            for(i=0; i<=k-1; i++)
            {bht[i]=bht[i+1];}
            bht[k-1]=tnt;
    
            //printing the outputs
            printf("Total no of predictions made\n%d",total);
            printf("no of correct\n%d no of wrong \n%d",correct, wrong);
            hitrate=(correct/total)*100;
            printf("hitrate is\n%f", hitrate);
            break;
          }
        case 3: 
          {
            int i,j,k,s,temp,n;
            int prediction;
            int total,correct,wrong;  
            float hitrate; 
            FILE *fp1,*fp2;
            int branchaddr,targetaddr,tnt,dec;
            k=atoi(argv[4]);
            s=atoi(argv[6]);
            j=powe(2,k);
            int pht[j];
            temp=s/k;
            int bht[temp][k]; 
            //initialising the history bits to 0 and PHT entries to 01 as given
            for(i=0;i<temp;i++)
            {
              for(n=0;n<k;n++) 
              {
                bht[i][n]=0;
              }
            }
            for(i=0;i<j;i++)
            {pht[i]=1;}
            //accessing the text file 
            fp1=fopen("history.txt","r");
            fp2=fopen("output.txt","w");
            while (fscanf(fp1,"%x %x %d",&branchaddr,&targetaddr,&tnt) != EOF)
            {
              if(s==32)
              {temp=branchaddr&31;}
              if(s==64)
              {temp=branchaddr&63;}
              if(s==128)
              {temp=branchaddr&127;}
              bin2dec(&dec,k,&bht[temp][0]);
              int index=dec;
              if(pht[index]==0 || pht[index]==1)
              {prediction=0;}
              if(pht[index]==2 || pht[index]==3)
              {prediction=1;}
              total=total+1; 
              if(tnt==prediction)
              correct++;
              if(tnt!=prediction) 
              wrong++;
              //am updating the counters based on the tnt values given.   
              if(tnt==1)
              {
                if (pht[index]<3)
                {
                  pht[index]++;
                }
              }
              if(tnt==0)
              {
                if(pht[index]>0)
                {pht[index]--;
                }
              }
              //updating the bht-usng ryt shift reg
              for(i=0;i<k-1;i++)
              {bht[temp][i]=bht[temp][i+1];}
              bht[temp][k-1]=tnt;
              bin2dec(&dec,k,&bht[temp][0]);
              //printing the outputs
              printf("Total no of predictions made\n%d",total);
              printf("no of correct\n%d no of wrong \n%d",correct, wrong);
              hitrate=(correct/total)*100;
              printf("hitrate is\n%f", hitrate);
              break;
            }
          case 4:
            {
              int i,j,k,temp,n;
              int prediction;
              int total,correct,wrong;
              int index;   
              float hitrate; 
              FILE *fp1,*fp2;
              int branchaddr,targetaddr,tnt,dec;
              k=atoi(argv[4]);
              j=powe(2,k);
              int pht[j],bht[k];
              //initialising the history bits to 0 and PHT entries to 01 as given
              for(i=0;i<k;i++)
              {bht[i]=0;}
              for(i=0;i<j;i++)
              {pht[i]=1;}
              while (fscanf(fp1,"%x %x %d",&branchaddr,&targetaddr,&tnt) != EOF)
              {
                if(k==4)
                {temp=branchaddr&15;}
                if(k==6)
                {temp=branchaddr&63;}
                if(k==7)
                {temp=branchaddr&127;}
                index=temp^dec; 
                //doing a prediction here
                if(pht[index]==0 || pht[index]==1)
                {prediction=0;}
                if(pht[index]==2 || pht[index]==3)
                {prediction=1;}
                total=total+1; 
                if(tnt==prediction)
                correct++;
                if(tnt!=prediction) 
                wrong++;
                //am updating the counters based on the tnt values given.   
                if(tnt==1)
                {
                  if (pht[index]<3)
                  {
                    pht[index]++;
                  }
                }
                if(tnt==0)
                {
                  if(pht[index]>0)
                  {pht[index]--;
                  }
                }
                //updating the bht-usng ryt shift reg
                for(i=0;i<k-1;i++)
                {bht[i]=bht[i+1];}
                bht[k-1]=tnt;
                i=bin2dec(&dec,k,bht);
              }
              //printing the outputs
              printf("Total no of predictions made\n%d",total);
              printf("no of correct\n%d no of wrong \n%d",correct, wrong);
              hitrate=(correct/total)*100;
              printf("hitrate is\n%f", hitrate);
              break;
            }
          }
        }
    Look CAREFULLY!
    See how case 3 and case 4 are indented more and more?
    See how the final closing brace isn't in column 1?

    That's right, your braces are all mis-matched.

    Tip:
    Always type your {} "" /**/ () in pairs, then move the cursor to fill in.
    Code:
    for ( ) {
    }
    then, and only then do you move the cursor and fill in the loop condition
    Code:
    for ( a ; b ; c ) {
    }
    At this point, you can press 'compile' to see that it compiles. If not, you FIX IT there and then.

    Then you add the body to the loop, compile and TEST
    Code:
    for ( a ; b ; c ) {
      // do some stuff
    }
    The point is, the code is never more than a few edits from being in a compilable state. If you rely on medium term memory to remember where to put all the braces, you're screwed.

    Measured progress is a skill
    A development process

    Dumping 300+ lines on a message board for someone else to fix is not a long term strategy.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    4
    It gives me segmentation fault.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    OK, but we still can't see your latest code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Feb 2010
    Posts
    4
    I am uploading the edited program again. I got no errors when I compiled it. But when I tried executing it, I get segmentation fault. Please help...

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    So do
    gcc -g prog1.c
    gdb a.out

    Then type
    run

    and wait for it to crash.
    Then you can examine the variables on the line of code gdb points you at, to figure out which pointer is garbage, or which array access is out of bounds.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. global namespace errors
    By stubaan in forum C++ Programming
    Replies: 9
    Last Post: 04-02-2008, 03:11 PM
  2. errors using my parser- MSVC++ 6, bhtypes.h
    By AeroHammer in forum C++ Programming
    Replies: 6
    Last Post: 01-25-2005, 07:11 PM
  3. opengl Nehe tutorial errors when compiling
    By gell10 in forum Game Programming
    Replies: 4
    Last Post: 07-14-2003, 08:09 PM
  4. executing errors
    By s0ul2squeeze in forum C++ Programming
    Replies: 3
    Last Post: 03-26-2002, 01:43 PM