Thread: A general protection exception failure

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    66

    Unhappy A general protection exception failure

    Hi, I am programming something to change from infix to postfix notation, However I get a general protection exception when i try to call the function that checs if the notation is correct.
    I don´t even know what a general protection exception is, can someone tell me please what is it, and also, can someone help me to get rid of it, i´ll place the first part of the code:


    Code:
    void cambio(int,int);    //esta funcion cambia entre las bases
    char *pedir_infija();    //esta funcion pide la expresion de forma infija
    char *posfija(char*);   //esta funcion cambia la expresion a postfija
    int comprobar(char*);   //comprueba que la expresi¢n se haya dado de manera correcta
    int eval(char *);        //Da el resultado de la postfija
    int convnum(char);       //Convierte el caracter a su numero equivalente
    
    void main(){
    	clrscr();
    	int comp=0,resul=0,op=0,z=1,yapos=0,yain=0,sicon=0,base=10;
    	char *expre, *postex;
    	while(op!=4){
    		op=0;
    		while(op>z||op<1){
    			z=1;
    			printf("\n\n---------Convertidor de expresiones---------\n");
    			printf("\n%d. Aceptar infija",z++);
    			if(yain==1){
    				printf("\n%d. Convertir a postfija",z++);
    			}
    			if (yapos==1){
    				if(resul==-60){
    					printf("\nOperacion invalida division entre cero");
    				}else{
    					printf("\n%d. Evaluar postfija",z++);
    				}
    			}
    			printf("\n%d. Salir\n\n Opcion: ",z);
    			scanf("%d",&op);
    			getchar();
    		}
    		if(op==z)op=4;
    		switch(op){
    			case 1:
    				comp=0;
    				while(comp==0){   //mientras no sea valida la expresion la pedirI am silly de nuevo
    					printf("\nDame la expresion infija: ");
    					scanf("%s",&expre);
    					getchar();
    					comp=comprobar(expre);      /* general protectio exception is here*/
    				}
    				//printf("\nLa expresi¢n infija es: %s",expre);
    				yain=1;
    				yapos=0;
    				break;
    			case 2:
    				postex=posfija(expre);   //llamada a la conversion a postfija
    				printf("Postfija: ");
    				printf("%s",postex);
    				resul=eval(postex);
    				yapos=1;
    				break;
    			case 3:
    				//calcula el resultado
    				printf("Resultado: %d",resul);
    				sicon=0;
    				while(sicon!=2){
    					sicon=0;
    					while(sicon>2||sicon<1){
    						printf("\nConvertir? (1.Si,2.No): ");
    						scanf("%d",&sicon);
    						getchar();
    					}
    					if(sicon!=2){
    						printf("\nEscribe la base: ");
    						scanf("%d",&base);
    						cambio(resul,base);
    					}
    				}
    				break;
    		}
    	}
    }
    
    
    
    
    
    int comprobar(char *expre){      /*here is where the function begins*/
    
    
    	int x,y=1,abr=0,cer=0;
    	for(x=0;x<strlen(expre);x++){   //va comprobando cada caracter
    		//comprueba de la lista de posibles caracteres que no haya ninguno
    		if(expre[x]!='+'&&expre[x]!='-'&&expre[x]!='*'&&expre[x]!='/'&&expre[x]!='%'&&expre[x]!='('&&expre[x]!=')'&&!isdigit(expre[x])) y=0;
    		if(expre[x]=='+'||expre[x]=='-'||expre[x]=='%'||expre[x]=='/'||expre[x]=='*'){     //En caso de que sea operador
    			if((!isdigit(expre[x-1])&&expre[x-1]!=')')||x==0){      //si el anterior a l operador no es un digito
    				y=0;
    			}
    		}
    		if(expre[x]=='('){     //cuenta parentesis abiertos
    			abr++;
    		}
    		if(expre[x]==')'){     //cuenta parentesis cerrados
    			cer++;
    		}
    		if(expre[x]==')'){     //verifica que cuando cierre parentesis lo anterior sea digito
    			if(!isdigit(expre[x-1])&&expre[x-1]!=')') y=0;
    		}
    	}
    	if(abr!=cer) y=0;  //si no son la misma cantidad de parentesis abiertos que cerrados
    	if(!isdigit(expre[strlen(expre)-1])&&expre[strlen(expre)-1]!=')') y=0;   //si el ultimo caracter es invalido
    	if(y==0) printf("Expresion Invalida\n\n");
    	return y;
    }
    
    }

    I wont´paste the rest of the code, since it is very long as you can see

    thank you

  2. #2
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    Use a debuger and see what happens step by step. The fault will happen somewhere in your code and you'll see where.

  3. #3
    Registered User
    Join Date
    Aug 2004
    Posts
    66
    I have added in the code the place where it marks the mistake the step by step debugging process,
    Code:
    comp=comprobar(expre);      /* general protection exception is here*/
    However i can't figure out what the mistake is.

    I have also marked the function it is calling.

  4. #4
    Registered User
    Join Date
    Aug 2004
    Posts
    66
    help please!

    I am really mad about this mistake

  5. #5
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    Please post code that compiles, and post ALL the functions, otherwise there's no way to us to find tou what's wrong. Don't repost your code, edit the 1st post.

  6. #6
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Bad:
    Code:
    	char *expre;
    	scanf("%s",&expre);
    Better:
    Code:
    	char expre[100];
    	scanf("%.99s", expre);
    Unreadable:
    Code:
    if(expre[x] != '+' &&expre[x]!='-'&&expre[x]!='*'&&expre[x]!='/'&&expre[x]!='%'&&expre[x]!='('&&expre[x]!=')'&&!isdigit(expre[x])) y=0;
    Readable:
    Code:
    if (!strchr("+-*/%()", expre[x]) && !isdigit(expre[x]))
    {
    	y = 0;
    }
    Code:
    if((!isdigit(expre[x-1])&&expre[x-1]!=')')||x==0){
    What happens if x is 0?


    --
    Last edited by anonytmouse; 11-24-2004 at 11:22 PM.

  7. #7
    ---
    Join Date
    May 2004
    Posts
    1,379
    [edit]
    nvm
    my bad
    (i cant find my reading glasses )

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > void main(){
    > clrscr();
    Wow - void main, and the need to attempt to use a C++ compiler to compile this C code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. general fault exception
    By chintugavali in forum C++ Programming
    Replies: 7
    Last Post: 04-26-2008, 11:02 AM
  2. Please assistance, general protection mistake
    By louis_mine in forum C++ Programming
    Replies: 3
    Last Post: 10-23-2004, 10:45 PM
  3. General Protection Exception
    By s9uare in forum C++ Programming
    Replies: 1
    Last Post: 11-02-2002, 10:46 AM
  4. general protection exception
    By chiw in forum C Programming
    Replies: 1
    Last Post: 08-14-2002, 09:54 AM
  5. General protection exception??
    By jonesy in forum C Programming
    Replies: 6
    Last Post: 10-10-2001, 12:34 AM