Unhandled exception

This is a discussion on Unhandled exception within the C Programming forums, part of the General Programming Boards category; Hi, I get an unhandled exception in this code: Code: while(strcmp(nom, linia_tren->nom_linia) != 0){ linia_tren++; } while((strcmp(parada1, linia_tren->dades_parades->nom_parada) != 0) ...

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    52

    Unhandled exception

    Hi, I get an unhandled exception in this code:
    Code:
    while(strcmp(nom, linia_tren->nom_linia) != 0){
        				   linia_tren++;
    			   }
    
    			   while((strcmp(parada1, linia_tren->dades_parades->nom_parada) != 0) && (strcmp(parada2, linia_tren->dades_parades->nom_parada) != 0)){
    				       linia_tren->dades_parades++;
    			   }
    			   
    			   if (strcmp(parada1, linia_tren->dades_parades->nom_parada) != 0){
    				   while(strcmp(parada2, linia_tren->dades_parades->nom_parada) != 0){
    					   linia_tren->dades_parades++;
    				   }
    				   
    			   }
    			   else{
                       while(strcmp(parada1, linia_tren->dades_parades->nom_parada) != 0){
    					   linia_tren->dades_parades++;
    				   }
    				  
    			   }
    Here my types declaration:

    Code:
    typedef struct
    {
       char nom_parada[MAX_NOM];
       int distancia_anterior;
       int temps_anterior;
    } parada;
    
    typedef struct
    {
       char nom_linia[MAX_NOM];
       int n_parades;
       parada *dades_parades;
    } linia;
    And variables:

    Code:
    char nom [MAX_NOM], parada1 [MAX_NOM], parada2 [MAX_NOM];
    int n_linia_tren
       linia *linia_tren;
    Thanks

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,303
    Frankly, your indentation is excessive and inconsistent. It would be more readable to write:
    Code:
    while (strcmp(nom, linia_tren->nom_linia) != 0) {
        linia_tren++;
    }
    
    while ((strcmp(parada1, linia_tren->dades_parades->nom_parada) != 0)
            && (strcmp(parada2, linia_tren->dades_parades->nom_parada) != 0)) {
        linia_tren->dades_parades++;
    }
    
    if (strcmp(parada1, linia_tren->dades_parades->nom_parada) != 0) {
        while (strcmp(parada2, linia_tren->dades_parades->nom_parada) != 0) {
            linia_tren->dades_parades++;
        }
    }
    else {
        while (strcmp(parada1, linia_tren->dades_parades->nom_parada) != 0) {
            linia_tren->dades_parades++;
        }
    }
    As for this "unhandled exception": try tracing your code with a debugger. At a glance, there is nothing obviously wrong with the code that you posted, so it would be best if you came up with the smallest and simplest compilable code that demonstrates the error.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,022
    I suspect I've seen this code before, but won't you mind showing a smallest possible compilable example that demonstrates the problem?
    You don't give us very much information to work with.

    With your current code, all I can say is:
    Warning 2 warning C4101: 'n_linia_tren' : unreferenced local variable g:\w00t\visual studio 2008\projects\temp\temp4.cpp 22
    Warning 3 warning C6001: Using uninitialized memory 'linia_tren': Lines: 21, 22, 23, 25 g:\w00t\visual studio 2008\projects\temp\temp4.cpp 25
    Warning 4 warning C4700: uninitialized local variable 'linia_tren' used g:\w00t\visual studio 2008\projects\temp\temp4.cpp 25
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Aug 2007
    Posts
    52
    This is the code that can be compiled:



    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define MAX_NOM 20
    #define TOTAL_LINIES 2
    
    typedef struct
    {
       char nom_parada[MAX_NOM];
       int distancia_anterior;
       int temps_anterior;
    } parada;
    
    typedef struct
    {
       char nom_linia[MAX_NOM];
       int n_parades;
       parada *dades_parades;
    } linia;
    
    
    void CalculaDisTemps(parada *parades, int longitud, float *distancia, float *temps){
    	int i;
        *distancia = 0;
    	*temps = 0;
    	i=0;
    
        do{
    	   *distancia = *distancia + (*parades).distancia_anterior;
           parades++;
           i++;
      	}while (i<longitud);
    	
    	
    	parades = parades - i;
    	i=0;
    
    	do{
    	   *temps = *temps + (*parades).temps_anterior;
           parades++;
           i++;
    	}while (i<longitud);
    
    
    
    }
    
    void OmplirDades (parada *parades, int nparades){
        int i;
           
    	for(i=0;i<nparades;i++){
    	   
    	   printf("Introdueixi el nom de la parada:\n");
    	   fgets(parades->nom_parada, MAX_NOM, stdin);
    	   printf("Introdueix la distància que hi ha des de la parada anterior:\n");
    	   scanf("%d", &parades->distancia_anterior);
    	   printf("Introdueix el temps que ha passat des de la parada anterior:\n");
    	   scanf("%d", &parades->temps_anterior);
    	   parades++;
        }
           parades = (parades - nparades);
    }
    
    
    void OmplirDadesLinia(linia *linia_tren){
    	
    	printf("Introdueix el nom de la línia:\n");
    	fgets (linia_tren->nom_linia, MAX_NOM, stdin);
    	printf("Introdueix el nombre de parades de la línia:\n");
    	scanf("%d", &linia_tren->n_parades);
    	linia_tren->dades_parades = (parada*)malloc(sizeof(parada)*linia_tren->n_parades);
    	OmplirDades (linia_tren->dades_parades, linia_tren->n_parades);
    
    }
    
    
    
    
    void main(){
    
       int n_linia_tren, i, menu, sortir;
       float distancia;
       float temps;
       char nom [MAX_NOM], parada1 [MAX_NOM], parada2 [MAX_NOM];
       linia *linia_tren;
          
       printf("Quantes línies de tren voleu tenir?");
       scanf("%d", &n_linia_tren);
       linia_tren = (linia*)malloc(sizeof(linia)*n_linia_tren);
    
       sortir = 1;
       
       do{
       
       printf("Selecciona una opció del menú:\n");
       scanf("%d", &menu);
    
       switch (menu){
    	   case 1:
    		   for (i=0; i<n_linia_tren; i++){
    		       OmplirDadesLinia(linia_tren);
    			   linia_tren++;
    		   }
    	   break;
    
    	   case 2: 
    		       printf("Introdueix el nom de la línia:\n");
    		       fgets(nom, MAX_NOM, stdin);
    			   printf("Introdueix el nom de dos parades d'aquesta línia:\n");
    			   fgets(parada1, MAX_NOM, stdin);
    			   fgets(parada2, MAX_NOM, stdin);
    			   while(strcmp(nom, linia_tren->nom_linia) != 0){
        				   linia_tren++;
    			   }
    
    			   while((strcmp(parada1, linia_tren->dades_parades->nom_parada) != 0) && (strcmp(parada2, linia_tren->dades_parades->nom_parada) != 0)){
    				       linia_tren->dades_parades++;
    			   }
    			   
    			   if (strcmp(parada1, linia_tren->dades_parades->nom_parada) != 0){
    				   while(strcmp(parada2, linia_tren->dades_parades->nom_parada) != 0){
    					   linia_tren->dades_parades++;
    				   }
    				   
    			   }
    			   else{
                       while(strcmp(parada1, linia_tren->dades_parades->nom_parada) != 0){
    					   linia_tren->dades_parades++;
    				   }
    				  
    			   }
    		break;
    	    case 3: sortir = 0;
       }  
                            
    	}while (sortir != 0);
    
    
       
    
       CalculaDisTemps(linia_tren->dades_parades, linia_tren->n_parades, &distancia, &temps);
    
       printf("%f\n", distancia);
       printf("%f\n", temps);
    
       free(linia_tren);
       
       
    }

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,022
    On first glance:
    Warning 1 warning C4326: return type of 'main' should be 'int' instead of 'void' g:\w00t\visual studio 2008\projects\temp\temp4.cpp 80
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,303
    This creates a linia:
    Code:
    linia_tren = (linia*)malloc(sizeof(linia)*n_linia_tren);
    However, the parada* belonging to this object is a null pointer, but you dereference it.

    Oh, and did you even pay attention when I told you to come up with the smallest and simplest compilable code that demonstrates the error?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Debugging and unhandled exception
    By Overlord in forum Windows Programming
    Replies: 2
    Last Post: 07-25-2008, 07:39 AM
  2. Replies: 3
    Last Post: 11-11-2006, 06:46 PM
  3. Unhandled exception: User breakpoint
    By glUser3f in forum C++ Programming
    Replies: 3
    Last Post: 10-02-2003, 05:20 PM
  4. unhandled exception error
    By trends in forum C++ Programming
    Replies: 4
    Last Post: 11-15-2002, 06:54 PM
  5. Unhandled Exception
    By StringQuartet13 in forum C++ Programming
    Replies: 1
    Last Post: 03-04-2002, 05:18 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21