2016-05-31 17 views
-2

Ich verstehe nicht, warum, wenn ich versuche Werte in archivio[] mit stampa Funktion zu drucken, wird dieses Programm drucktWarum druckt dieser Code einige Werte nicht?

"studente","matricola","nome","cognome"

richtig, aber Werte nicht von stampaEsami drucken.

#include <stdio.h> 
#include <stdlib.h> 
#define MAXSTUDENTI 20 
#define MAXSTRINGA 100 
#define MAXESAMI 25 

/* run this program using the console pauser or add your own getch, system("pause") or input loop */ 

typedef char Stringa[MAXSTRINGA]; 

typedef enum { uno, due, tre, FC 
} AnnoCorso; 

typedef struct { 
    Stringa nomeEsame; 
    int voto; 
} Esame; 

typedef struct { 
    Esame listaEsami[MAXESAMI]; 
    int numeroEsami; 
}ListaEsame; 

typedef struct { 
    int matricola; 
    Stringa nome; 
    Stringa cognome; 
    AnnoCorso anno; 
    ListaEsame esami; 
} Studente; 

void init(Studente[], int); 
void acquisisciEsami(Studente, int); 
void stampa(Studente[], int); 
void stampaEsami(ListaEsame); 

void init(Studente archivio[], int n){ 

    int i; 
    int nEsami; 

    for(i = 0; i < n; i++){ 
     printf("Studente n. %d\n", i+1); 
     printf("Inserire matricola: "); 
     scanf("%d", &archivio[i].matricola); 
     printf("Inserire nome: "); 
     scanf("%s", &archivio[i].nome); 
     printf("Inserire cognome: "); 
     scanf("%s", &archivio[i].cognome); 
     printf("Inserire il numero di esami svolti: "); 
     scanf("%d", &archivio[i].esami.numeroEsami); 
     nEsami = archivio[i].esami.numeroEsami; 
     if(nEsami != 0) { 
      acquisisciEsami(archivio[i], nEsami); 
     } 

    } 

} 

void acquisisciEsami(Studente studente, int n){ 

    int i; 

    for(i = 0; i < n; i++) { 
     printf("Inserire nome esame:"); 
     scanf("%s", studente.esami.listaEsami[i].nomeEsame); 
     printf("Inserire voto esame:"); 
     scanf("%d", &studente.esami.listaEsami[i].voto); 
    } 

} 

void stampa(Studente archivio[], int n){ 

    printf("\nGli studenti presenti in archivio sono:\n"); 

    int i; 

    for(i = 0; i < n; i++){ 
     printf("Studente n. %d:\n", i+1); 
     printf("Matricola: %d\n", archivio[i].matricola); 
     printf("Nome: %s\n", archivio[i].nome); 
     printf("Cognome: %s\n", archivio[i].cognome); 
     stampaEsami(archivio[i].esami); 
    } 
} 

void stampaEsami(ListaEsame esami){ 

    int i = 0; 
    int n = esami.numeroEsami; 

    for(i = 0; i < n; i++){ 
     printf("Nome esame: %s\n", esami.listaEsami[i].nomeEsame); 
     printf("Voto esame: %d\n", esami.listaEsami[i].voto); 
    } 
} 

int main(int argc, char *argv[]) { 

    Studente studenti[MAXSTUDENTI] ; 
    int n; 

    printf("Inserire il numero di studenti da memorizzare in archivio:\n "); 
    scanf("%d", &n); 
    init(studenti, n); 
    stampa(studenti, n); 

    return 0; 
} 

Wenn der Eingang ist:

Inserire il numero di studenti da memorizzare in archivio:1 
Inserire matricola: 13434 
Inserire nome: test 
Inserire cognome: test 
Inserire il numero di numero di esami svolti: 1 
Inserire nome esame: asd2 
Inserire voto esame: 20 

druckt:

Gli studenti presenti in archivio sono: 
Studente n.1: 
Matricola: 13434 
Nome : test 
Cognome: test 
Nome esame: 
Voto esame: 0 
+0

Sie etwas dagegen die Schaffung eines [___MCVE___] (http://stackoverflow.com/help/mcve) sein? –

+0

Ich denke, dass alle Code, den ich schrieb, dient, um das Problem zu laufen und zu verstehen –

+1

Für einige spezifische Eingabe, was ist die tatsächliche Ausgabe, und was ist die erwartete Ausgabe? Bitte fügen Sie es ein, indem Sie die tatsächliche Eingabe/Ausgabe kopieren. –

Antwort

3

Ihr Problem ist acquisisciEsami Funktion.

Es sollte Studente * akzeptieren, anstelle einer Variablen als Wert übergeben.

void acquisisciEsami(Studente *studente, int n) 
{ 
    int i; 

    for(i = 0; i < n; i++) 
    { 
     printf("Inserire nome esame:"); 
     scanf("%s", studente->esami.listaEsami[i].nomeEsame); 
     printf("Inserire voto esame:"); 
     scanf("%d", &studente->esami.listaEsami[i].voto); 
    } 
} 

Also, was ist das Problem? Ihr Code kompiliert eine Struktur mit lokalem Gültigkeitsbereich in die Funktion acquisisciEsami und alle Daten gehen verloren, wenn die Funktion endet. So wird esami Mitglied von nicht geändert.

Passing archivio[i] unter Bezugnahme auf acquisisciEsami Sie Inhalte von esami memeber archivio[i]

Offensichtlich ist der Aufruf acquisisciEsami wird ändern können:

acquisisciEsami(&archivio[i], nEsami); 

Wie ich kommentiert, das Sie Problem haben zu beheben:

  • scanf("%s", &archivio[i].nome); muss scanf("%s",archivio[i].nome);
  • sein
  • scanf("%s", &archivio[i].cognome); sollte scanf("%s",archivio[i].cognome);