Oppure

Loading
05/03/11 10:35
Lego86
Ciao a tutti!
Ho scritto un programma che carica una lista con dei nomi e delle età. Il programma dovrebbe stampare l'elemento con età maggiore, ma quando provo a stampare a video o a fare il controllo dell'età sui singoli nodi mi dà un errore di memoria.. potreste darmi una mano? :(

Grazie mille,
Roberto

main.cpp
#include <cstdlib>
#include <iostream>
#include "header.h"
using namespace std;

int main(int argc, char *argv[])
{
    pNodo testa;
    inizializzaLista();
    testa=inserisciInLista(testa);
    stampaLista(testa);
    trovaMassimo(testa);
    system("PAUSE");
    return EXIT_SUCCESS;
}


header.h
#ifndef ESAMELISTA_H
#define ESAMELISTA_H
#define N 100
typedef char stringa[N];
typedef struct nodo *pNodo;
typedef struct nodo{
   stringa nome;
   int eta;
   pNodo next;
};

pNodo inizializzaLista();
pNodo push(pNodo,stringa,int);
pNodo inserisciInLista(pNodo);
void trovaMassimo(pNodo);
void stampaLista(pNodo);
#endif


header.cpp
#include <iostream>
#include "header.h"
using namespace std;

pNodo inizializzaLista(){
   pNodo p=new nodo;
   p=0;
   return p;
}

pNodo push(pNodo testa,stringa name,int age){
   pNodo p=new nodo;
   strcpy(p->nome,name);
   p->eta=age;
   p->next=testa;
   return p;
}
pNodo inserisciInLista(pNodo testa){
   stringa name;
   int n,age;
   cout<<"Quante persone vuoi inserire nella lista?"<<endl;
   cin>>n;
   for(int i=0;i<n;i++){
      cout<<"Inserici nome "<<i+1<<": ";
      cin>>name;
      cout<<"Inserici eta "<<i+1<<": ";
      cin>>age;
      testa=push(testa,name,age);
   }
   return testa;
}

void trovaMassimo(pNodo testa){
   int maxEta=0;
   string maxNome;
   while(testa!=0){
   int listAge=testa->eta;
     if(maxEta<(listAge)){
         maxEta=testa->eta;
         maxNome=testa->nome;
      }
      testa=testa->next;   
   }
   cout<<"La persona piu' grande e' "<<maxNome<<" di anni "<<maxEta<<endl;
}

void stampaLista(pNodo testa){
   cout<<"La lista e':"<<endl;
   while(testa!=0){
      cout<<testa->nome<<endl;
      cout<<testa->eta<<endl;
      testa=testa->next;
   }
}
aaa
05/03/11 16:42
lorenzo
semplice, hai dimenticato il valore di ritorno nel main:

#include <cstdlib>
#include <iostream>
#include "header.h"
using namespace std;
 
int main(int argc, char *argv[])
{
    pNodo testa;
    testa = inizializzaLista(); //IL VALORE DI RITORNO
    testa=inserisciInLista(testa);
    stampaLista(testa);
    trovaMassimo(testa);
    system("PAUSE");
    return EXIT_SUCCESS;
}
aaa
06/03/11 9:13
Lego86
Che cretino.. grazie mille!:k:
aaa