Help

This is a discussion on Help within the C++ Programming forums, part of the General Programming Boards category; Please check this code and tell me what's the problem with it... #include <iostream.h> #include <stdlib.h> #include <fstream.h> #include <string.h> ...

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    7

    Question Help

    Please check this code and tell me what's the problem with it...

    #include <iostream.h>
    #include <stdlib.h>
    #include <fstream.h>
    #include <string.h>

    struct nodo1{
    char *nombre;
    int contador;
    struct nodo1 *next;
    struct nodo2 *sig;
    };

    struct nodo2{
    int x;
    struct nodo2 *sig;
    };

    typedef struct nodo1 reg1;
    typedef struct nodo2 reg2;

    reg2* nuevo_linea(int linea){
    reg2 *nuevo2;
    nuevo2=(reg2 *)new reg2;
    nuevo2->x=linea;
    nuevo2->sig=NULL;
    return nuevo2;
    }

    reg1* nuevo_nodo1(char *a,int tam,int linea){
    reg1 *nuevo;
    nuevo=(reg1 *)new reg1;
    nuevo->nombre=new char[tam];
    strcpy(nuevo->nombre,a);
    nuevo->contador=1;
    nuevo->next=NULL;
    reg2 *nuevo_lista=nuevo_linea(linea);
    nuevo->sig=nuevo_lista;
    return nuevo;
    }

    void main(){
    reg1 *lista=NULL;
    char a[10];
    int linea=1,tam;
    ifstream archivo("entrada2.txt");
    while(!archivo.eof()){
    archivo>>a;
    tam=strlen(a);
    reg1 *aux=lista;
    if(a[0]==13)//sumar las lineas
    linea++;
    reg1 *nuevo=nuevo_nodo1(a,tam,linea);
    if(lista==NULL){//crear primer elemento
    lista=nuevo;
    }
    if(strcmp(nuevo->nombre,lista->nombre)<0){
    nuevo->next=lista;
    lista=nuevo;
    }
    while(aux->next!=NULL){
    if(strcmp(nuevo->nombre,aux->nombre)==0){
    aux->contador++;
    reg2 *nuevo2=nuevo_linea(linea);
    reg2 *aux2=aux->sig;
    while(aux->sig->sig!=NULL){
    aux2=aux2->sig;
    }
    aux2->sig=nuevo2;
    }
    else
    aux=aux->next;
    }
    aux=lista;
    while(aux->next!=NULL){
    if((strcmp(nuevo->nombre,aux->next->nombre))>0)
    aux=aux->next;
    else
    break;
    }
    nuevo->next=aux->next;
    aux->next=nuevo;
    }
    archivo.close();
    reg1 *aux=lista;
    reg2 *aux2;
    aux2=aux->sig;
    while(aux->next!=NULL){
    cout<<endl<<" | "<<aux->nombre<<" | "<<aux->contador;
    while(aux2->sig!=NULL){
    cout<<" | "<<aux2->x;
    if(aux2->sig->sig!=NULL)
    cout<<", ";
    aux2=aux2->sig;
    }
    aux=aux->next;
    aux2=aux->sig;
    }
    }

  2. #2
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    At the begining of main you do -

    reg1 *lista=NULL;

    before lista points to a valid location you do -

    reg1 *aux=lista;

    So aux now points to NULL. Without pointing aux anywhere else you do -

    while(aux->next!=NULL)

    and aux doesn't have a next(that points to anything meaningful) because next points to NULL so your program will crash.
    zen

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    7

    Question Another problem

    Thnks for the last msg, I changed the code, it didn't crash but when i execute it, it doesn't print anything, please copy it and try to fix it, you need a file named entrada.txt, it should have some names (put some repeated names), the program should print the names, the number of times it appeared and the lines where it was each time... please help me...

    #include <iostream.h>
    #include <stdlib.h>
    #include <fstream.h>
    #include <string.h>

    struct nodo1{
    char *nombre;
    int contador;
    struct nodo1 *next;
    struct nodo2 *sig;
    };

    struct nodo2{
    int x;
    struct nodo2 *sig;
    };

    typedef struct nodo1 reg1;
    typedef struct nodo2 reg2;

    reg2* nuevo_linea(int linea){
    reg2 *nuevo2;
    nuevo2=(reg2 *)new reg2;
    nuevo2->x=linea;
    nuevo2->sig=NULL;
    return nuevo2;
    }

    reg1* nuevo_nodo1(char *a,int tam,int linea){
    reg1 *nuevo;
    nuevo=(reg1 *)new reg1;
    nuevo->nombre=new char[tam];
    strcpy(nuevo->nombre,a);
    nuevo->contador=1;
    nuevo->next=NULL;
    reg2 *nuevo_lista=nuevo_linea(linea);
    nuevo->sig=nuevo_lista;
    return nuevo;
    }
    void mayor(reg1 *aux,reg1 *nuevo){
    while(aux->next!=NULL){
    if((strcmp(nuevo->nombre,aux->nombre))>0)
    aux=aux->next;
    else{
    nuevo->next=aux->next;
    aux->next=nuevo;
    aux=aux->next;
    break;
    }
    }
    }

    void main(){
    reg1 *lista=NULL;
    reg1 *aux;
    char a[10];
    int linea=1,tam,primero;
    ifstream archivo("entrada.txt");
    while(!archivo.eof()){
    archivo>>a;
    tam=strlen(a);
    if(a[0]==13)//sumar las lineas
    linea++;
    reg1 *nuevo=nuevo_nodo1(a,tam,linea);
    aux=lista;
    if(lista==NULL){//crear primer elemento
    lista=nuevo;
    char k[10]="";
    reg1 *l=nuevo_nodo1(k,tam,linea);
    lista->next=l;
    primero=1;
    }
    aux=lista;
    if(strcmp(nuevo->nombre,lista->nombre)<0){
    nuevo->next=lista;
    lista=nuevo;
    }
    reg1 *ax=lista;
    while(ax->next!=NULL){
    if(strcmp(nuevo->nombre,ax->nombre)==0&&primero!=1){
    ax->contador++;
    reg2 *nuevo2=nuevo_linea(linea);
    reg2 *b=ax->sig;
    if(ax->contador>1){
    while(b->sig!=NULL){
    b=b->sig;
    }
    b->sig=nuevo2;
    }
    else
    b->sig=nuevo2;
    }
    ax=ax->next;
    }
    primero=0;
    ax=lista;
    mayor(ax,nuevo);
    }
    archivo.close();
    aux=lista;
    reg2 *aux2;
    aux2=aux->sig;
    while(aux->next!=NULL){
    cout<<endl<<" | "<<aux->nombre<<" | "<<aux->contador;
    while(aux2->sig!=NULL){
    cout<<" | "<<aux2->x;
    if(aux2->sig->sig!=NULL)
    cout<<", ";
    aux2=aux2->sig;
    }
    aux=aux->next;
    aux2=aux->sig;
    }
    }
    Last edited by Atomsk; 09-26-2001 at 07:53 PM.

  4. #4
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    I've re-written most of your main as I couldn't work out exactly what you were doing, but I think this is the kind of thing your after.

    I would recommend wrapping this in some kind of class, or at least breaking main up into separate functions as all the nested loops make it pretty hard to debug.

    I've replaced your file input for console input (ctrl-z to end), and I've been unable to delete the memory allocated for each name using MSVC in debug mode (not sure what I've overlooked) so if you want to debug the program as it stands you'll have to comment out the lines

    delete [] nuevo->nombre; // in block for duplicate name check

    and

    delete [] aux->nombre; // in final cleanup block



    Code:
    #include <iostream> 
    #include <cstring>
    
    using namespace std;
    
    struct nodo1{ 
    	char *nombre; 
    	int contador; 
    	struct nodo1 *next; 
    	struct nodo2 *sig; 
    }; 
    
    struct nodo2
    { 
    	int x; 
    	struct nodo2 *sig; 
    }; 
    
    typedef struct nodo1 reg1; 
    typedef struct nodo2 reg2; 
    
    reg2* nuevo_linea(int linea)
    { 
    	reg2 *nuevo2; 
    	nuevo2=new reg2; 
    	nuevo2->x=linea; 
    	nuevo2->sig=NULL; 
    	return nuevo2; 
    } 
    
    reg1* nuevo_nodo1(char *a,int tam,int linea)
    { 
    	reg1 *nuevo; 
    	nuevo=new reg1; 
    	nuevo->nombre=new char[tam]; 
    	strcpy(nuevo->nombre,a); 
    	nuevo->contador=1; 
    	nuevo->next=NULL; 
    	reg2 *nuevo_lista=nuevo_linea(linea); 
    	nuevo->sig=nuevo_lista; 
    	return nuevo; 
    } 
    void mayor(reg1*&aux,reg1*&nuevo)
    { 
    	while(aux!=NULL)
    	{ 
    		if(aux->next==NULL)
    			{
    				aux->next=nuevo;
    				break;
    			}
    				
    		else if(strcmp(nuevo->nombre,aux->next->nombre)<0)
    		{ 
    		nuevo->next=aux->next; 
    		aux->next=nuevo; 
    		aux=aux->next; 
    		break; 
    		} 
    
    		aux=aux->next;
    	} 
    } 
    
    
    
    int main()
    {
    	
    	int linea =1;
    	reg1* lista=NULL;
    	reg1* nuevo;
    	reg1* aux;
    	char nombre[20]={0};
    	int tam;
    
    	while (1)
    	{
    		cin.get(nombre,19,'\n');
    		cin.ignore(80,'\n');
    		if (cin.eof())break;
    
    		tam=strlen(nombre);
    
    		nuevo = nuevo_nodo1(nombre,tam,linea);
    
    		if(lista==NULL)
    		{
    			lista = nuevo;
    			linea++;
    			continue;
    		}
    
    		aux= lista;
    
    		while(aux!=NULL)
    		{
    			if (strcmp(nuevo->nombre,aux->nombre)==0)
    			{
    				reg2* nuevo2 = new reg2;
    				nuevo2->x=nuevo->sig->x;
    				nuevo2->sig=NULL;
    				
    				
    				while(aux->sig->sig!=NULL)
    					aux->sig = aux->sig->sig;
    
    				aux->sig->sig=nuevo2;
    				aux->contador++;
    				linea++;
    				delete [] nuevo->nombre;
    				delete nuevo->sig;
    				delete nuevo;
    				nuevo = NULL;
    				break;
    
    			}
    			aux=aux->next;
    		}
    		if(nuevo ==NULL)
    			continue;
    
    		
    
    		if (strcmp(nuevo->nombre,lista->nombre)<0)
    		{
    			nuevo->next=lista;
    			lista=nuevo;
    			linea++;
    			continue;
    		}
    
    		aux=lista;
    
    		mayor(aux,nuevo);
    		linea++;
    
    
    	}
    
    
    	aux=lista;
    	while(aux!=NULL)
    	{
    		cout << aux->nombre << " Contador - " << aux->contador << " Linea - ";
    
    		while(aux->sig!=NULL)
    		{
    			cout << aux->sig->x << " - ";
    			aux->sig=aux->sig->sig;
    		}
    
    		cout << endl;
    
    		aux=aux->next;
    	}
    
    
    	aux=lista;
    	
    	while(aux!=NULL)
    	{
    		reg1* aux2 = aux->next;
    		delete [] aux->nombre;
    		while(aux->sig!=NULL)
    		{
    			reg2* aux3 = aux->sig->sig;
    			delete aux->sig;
    			aux->sig=aux3;
    		}
    		delete aux;
    		aux=aux2;
    	}
    		
    
    	return 0;
    
    }
    zen

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    7

    Thnks

    thnks for the help, I executed it and it worked fine. But it doesn't show all the lines where the string was found... I tried to fix it but I just can't... please help...

    sorry for the problems, but i'm new with lists and I just can't figure out its behaiviour...

  6. #6
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    But it doesn't show all the lines where the string was found...
    Yes it does. When the input has finished it will show the count after Contador and then a series of numbers separated by '-' after Linea showing which lines the name was on.
    zen

Popular pages Recent additions subscribe to a feed

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