Visualizzazione dei risultati da 1 a 10 su 10

Discussione: probl. ALGORITMO "C"

  1. #1

    probl. ALGORITMO "C"

    Devo presentare un programma per domani!
    E' perfetto ma non riesco a trovare l'errore!

    Che faccio!???


    Unisce 2 array di struct (studente) e deve avere la possibilità di scegliere da tastiera quale compo si vuole unire in un terzo array (risultato).

    #include<stdio.h>
    #include<conio.h>
    #include<string.h>
    struct studente {
    char nome[];
    char cognome[];
    int matricola;
    } ;
    struct risultato {
    char stringa [];
    int matricola;
    };
    void riempi(studente str[],int n);
    void unione_cognome(studente str1[],studente str2[],risultato unione[],int n1,int n2);
    void unione_nome(studente str1[],studente str2[],risultato unione[],int n1,int n2);
    void unione_int(studente str1[],studente str2[],studente unione[],int n1,int n2);
    int scelta(studente str1[],studente str2[],risultato unione[],int n1,int n2);
    void risulta(risultato unione[],int n1,int n2,int scelto);
    void main()
    {
    int num1,num2,scelto;
    char vai;
    studente str1[25],str2[30];
    risultato unione[55];
    printf("\nInserisci il numero di studenti del primo vettore:\n");
    scanf("%d",&num1);
    riempi(str1,num1);
    printf("\nIl primo vettore è pieno");
    printf("\nInserisci il numero di studenti del secondo vettore:\n");
    scanf("%d",&num2);
    riempi(str2,num2);
    printf("\nIl secondo vettore è pieno");
    scelto=scelta(str1,str2,unione,num1,num2);
    printf("\nIl risultato è raggiunto premi un tasto per visualizzarlo");
    vai=getch();
    risulta(unione,num1,num2,scelto);
    }
    void riempi(studente vet[30],int n)
    {
    int i;
    for(i=0;i<n;i++)
    {
    printf("\nInserisci il Cognome del %d° allievo: ",i+1);
    scanf("%s",&vet[i].cognome);
    printf("\nInserisci il Nome del %d° allievo: ",i+1);
    scanf("%s",&vet[i].nome);
    printf("\nInseriscine ora la matricola: ");
    scanf("%d",&vet[i].matricola);
    clrscr();
    }
    }
    void unione_cognome(studente str1[25],studente str2[30],risultato unione[55],int n1,int n2)
    {
    int i,j;
    for(i=0;i<n1;i++)
    strcpy(unione[i].stringa,str1[i].cognome);
    j=i;
    for(i=0;i<n2;i++)
    {
    strcat(unione[j].stringa,str2[i].cognome);
    j++;
    }
    }
    void unione_nome(studente str1[25],studente str2[30],risultato unione[55],int n1,int n2)
    {
    int i,j;
    for(i=0;i<n1;i++)
    strcpy(unione[i].stringa,str1[i].nome);
    j=i;
    for(i=0;i<n2;i++)
    {
    strcat(unione[j].stringa,str2[i].nome);
    j++;
    }
    }
    void unione_int(studente str1[25],studente str2[30],risultato unione[55],int n1,int n2)
    {
    int i,j;
    for(i=0;i<n1;i++)
    unione[i].matricola=str1[i].matricola;
    j=i;
    for(i=0;i<n2;i++){
    unione[j].matricola=str2[i].matricola;
    j++; }

    }
    int scelta(studente str1[25],studente str2[30],risultato unione[55],int num1,int num2)
    {
    int scelto,i=0;
    printf("\n\t\tScegli quale di quale campo vuoi l'unione :\n\n\n");
    printf("\n\t 1 : Il campo matricola ");
    printf("\n\t 2 : Il campo cognome ");
    printf("\n\t 3 : Il campo nome\n\t");
    scanf("%d",&scelto);
    do
    {
    if(i)
    {
    printf("\n Non puoi inserire un numero non compreso tra l'intervallo 1 - 3");
    scanf("%d",&scelto); }
    switch(scelto){
    case 1:
    unione_int(str1,str2,unione,num1,num2);
    break;
    case 2:
    unione_cognome(str1,str2,unione,num1,num2);
    break;
    case 3:
    unione_nome(str1,str2,unione,num1,num2);
    break;
    default:
    i=1;
    } }
    while(i);
    return scelto;
    }
    void risulta(risultato unione[55],int n1,int n2,int scelto)
    {
    int i;
    clrscr();
    if(scelto==2||3)
    for(i=0;i<(n1+n2);i++)
    printf("\n\t%s",unione[i].stringa);
    else
    for(i=0;i<(n1+n2);i++)
    printf("\n\t%d",unione[i].matricola);
    }

  2. #2
    sarebbe preferibile se usassi il tag per rendere leggibile il codice, così come l'hai scritto te non si capisce un piffero.
    Il tag è
    [*CODE](senza *)
    codice
    [/*CODE] (senza *)
    rappresentato dal bottone #

    05.08.2005 - by alka
    Auguri all'angelo custode dei moderatori.

  3. #3
    Utente di HTML.it L'avatar di pdpmpd
    Registrato dal
    Jan 2003
    Messaggi
    448
    intanto metti le keywords "struct" quando dichiari una struttura (non hai fatto il typedef).
    Drug misuse is not a disease, it is a decision, like the decision to step out in front of a moving car. [...] In this particular life-style the motto is "be happy now because tomorrow you are dying", but the dying begins almost at once, and the happiness is a memory.

  4. #4
    scusa, non sono pratico!

    codice:
     #include<stdio.h>
     #include<conio.h>
     #include<string.h>
       struct studente {
    	char nome[];
    	char cognome[];
    	int  matricola;
    	} ;
     struct risultato {
    	char stringa [];
    	int  matricola;
    	};
     void riempi(studente str[],int n);
     void unione_cognome(studente str1[],studente str2[],risultato unione[],int n1,int n2);
     void unione_nome(studente str1[],studente str2[],risultato unione[],int n1,int n2);
     void unione_int(studente str1[],studente str2[],studente unione[],int n1,int n2);
     int scelta(studente str1[],studente str2[],risultato unione[],int n1,int n2);
     void risulta(risultato unione[],int n1,int n2,int scelto);
     void main()
      {
    	  int num1,num2,scelto;
    	  char vai;
    	  studente str1[25],str2[30];
    	  risultato unione[55];
    	  printf("\nInserisci il numero di studenti del primo vettore:\n");
    	  scanf("%d",&num1);
    	  riempi(str1,num1);
    	  printf("\nIl primo vettore è pieno");
    	  printf("\nInserisci il numero di studenti del secondo vettore:\n");
    	  scanf("%d",&num2);
    	  riempi(str2,num2);
    	  printf("\nIl secondo vettore è pieno");
    	  scelto=scelta(str1,str2,unione,num1,num2);
    	  printf("\nIl risultato è raggiunto premi un tasto per visualizzarlo");
    	  vai=getch();
    	  risulta(unione,num1,num2,scelto);
      }
     void riempi(studente vet[30],int n)
      {
    	int i;
    	for(i=0;i<n;i++)
    		{
    		  printf("\nInserisci il Cognome del %d° allievo: ",i+1);
    		  scanf("%s",&vet[i].cognome);
    		  printf("\nInserisci il Nome del %d° allievo: ",i+1);
    		  scanf("%s",&vet[i].nome);
    		  printf("\nInseriscine ora la matricola: ");
    		  scanf("%d",&vet[i].matricola);
    		  clrscr();
    		}
    	}
     void unione_cognome(studente str1[25],studente str2[30],risultato unione[55],int n1,int n2)
    	{
    		int i,j;
    		for(i=0;i<n1;i++)
    			  strcpy(unione[i].stringa,str1[i].cognome);
    		j=i;
    		for(i=0;i<n2;i++)
    			 {
    			  strcat(unione[j].stringa,str2[i].cognome);
    			  j++;
    			 }
    	}
    	void unione_nome(studente str1[25],studente str2[30],risultato unione[55],int n1,int n2)
    	{
    		int i,j;
    		for(i=0;i<n1;i++)
    			  strcpy(unione[i].stringa,str1[i].nome);
    		j=i;
    		for(i=0;i<n2;i++)
    			 {
    			  strcat(unione[j].stringa,str2[i].nome);
    			  j++;
    			 }
    	}
     void unione_int(studente str1[25],studente str2[30],risultato unione[55],int n1,int n2)
    	{
    		int i,j;
    		for(i=0;i<n1;i++)
    			  unione[i].matricola=str1[i].matricola;
    		j=i;
    		for(i=0;i<n2;i++){
    			  unione[j].matricola=str2[i].matricola;
    			  j++; }
    
    	}
     int scelta(studente str1[25],studente str2[30],risultato unione[55],int num1,int num2)
    	{
    	 int scelto,i=0;
    	 printf("\n\t\tScegli quale di quale campo vuoi l'unione :\n\n\n");
    	 printf("\n\t 1  :  Il campo matricola ");
    	 printf("\n\t 2  :  Il campo cognome ");
    	 printf("\n\t 3  :  Il campo nome\n\t");
    	  scanf("%d",&scelto);
    	 do
    	  {
    		if(i)
    			{
    			  printf("\n   Non puoi inserire un numero non compreso tra l'intervallo 1 - 3");
    			  scanf("%d",&scelto);  }
    		 switch(scelto){
    			 case 1:
    				unione_int(str1,str2,unione,num1,num2);
    			 break;
    			 case 2:
    				unione_cognome(str1,str2,unione,num1,num2);
    			 break;
    			 case 3:
    				unione_nome(str1,str2,unione,num1,num2);
    			 break;
    			 default:
    				i=1;
    		  }  }
    		 while(i);
    		return scelto;
    		}
    	void risulta(risultato unione[55],int n1,int n2,int scelto)
    		{
    		 int i;
    		 clrscr();
    		 if(scelto==2||3)
    			for(i=0;i<(n1+n2);i++)
    			  printf("\n\t%s",unione[i].stringa);
    		 else
    			for(i=0;i<(n1+n2);i++)
    			  printf("\n\t%d",unione[i].matricola);
    		}

  5. #5
    Utente di HTML.it L'avatar di pdpmpd
    Registrato dal
    Jan 2003
    Messaggi
    448
    questi son un po' di errori che mi ha dato a compilare (gli sto correggiendo via via):
    codice:
    trip.c: In function `main':
    trip.c:21: warning: return type of `main' is not `int'
    trip.c: At top level:
    trip.c:41: parse error before "vet"
    trip.c: In function `riempi':
    trip.c:42: number of arguments doesn't match prototype
    trip.c:13: prototype declaration
    trip.c:44: `n' undeclared (first use in this function)
    trip.c:44: (Each undeclared identifier is reported only once
    trip.c:44: for each function it appears in.)
    trip.c:47: `vet' undeclared (first use in this function)
    trip.c: At top level:
    trip.c:55: parse error before "str1"
    trip.c: In function `unione_cognome':
    trip.c:56: number of arguments doesn't match prototype
    trip.c:14: prototype declaration
    trip.c:58: `n1' undeclared (first use in this function)
    trip.c:59: `unione' undeclared (first use in this function)
    trip.c:59: `str1' undeclared (first use in this function)
    trip.c:61: `n2' undeclared (first use in this function)
    trip.c:63: `str2' undeclared (first use in this function)
    trip.c: At top level:
    trip.c:67: parse error before "str1"
    trip.c: In function `unione_nome':
    trip.c:68: number of arguments doesn't match prototype
    trip.c:15: prototype declaration
    trip.c:70: `n1' undeclared (first use in this function)
    trip.c:71: `unione' undeclared (first use in this function)
    trip.c:71: `str1' undeclared (first use in this function)
    trip.c:73: `n2' undeclared (first use in this function)
    trip.c:75: `str2' undeclared (first use in this function)
    trip.c: At top level:
    trip.c:79: parse error before "str1"
    trip.c: In function `unione_int':
    trip.c:80: number of arguments doesn't match prototype
    trip.c:16: prototype declaration
    trip.c:82: `n1' undeclared (first use in this function)
    trip.c:83: `unione' undeclared (first use in this function)
    trip.c:83: `str1' undeclared (first use in this function)
    trip.c:85: `n2' undeclared (first use in this function)
    trip.c:86: `str2' undeclared (first use in this function)
    trip.c: At top level:
    trip.c:90: parse error before "str1"
    trip.c: In function `scelta':
    trip.c:91: number of arguments doesn't match prototype
    trip.c:17: prototype declaration
    trip.c:106: `str1' undeclared (first use in this function)
    trip.c:106: `str2' undeclared (first use in this function)
    trip.c:106: `unione' undeclared (first use in this function)
    trip.c:106: `num1' undeclared (first use in this function)
    trip.c:107: `num' undeclared (first use in this function)
    trip.c:107: parse error before numeric constant
    trip.c:111: `num2' undeclared (first use in this function)
    trip.c:115: `nu' undeclared (first use in this function)
    trip.c:115: parse error before "m2"
    trip.c: At top level:
    trip.c:123: parse error before "unione"
    trip.c: In function `risulta':
    trip.c:124: number of arguments doesn't match prototype
    trip.c:18: prototype declaration
    trip.c:127: `scelto' undeclared (first use in this function)
    trip.c:128: `n1' undeclared (first use in this function)
    trip.c:128: `n2' undeclared (first use in this function)
    trip.c:129: `unione' undeclared (first use in this function)
    Drug misuse is not a disease, it is a decision, like the decision to step out in front of a moving car. [...] In this particular life-style the motto is "be happy now because tomorrow you are dying", but the dying begins almost at once, and the happiness is a memory.

  6. #6
    a me non da errori.

    Che faccio quindi ?

    In esecuzione mi da valori sporchi.

  7. #7
    Utente di HTML.it L'avatar di pdpmpd
    Registrato dal
    Jan 2003
    Messaggi
    448
    comunque in teoria devi metter gli struct quando dichiari una variabile di tipo struct.
    ora controllo l'esecuzione...
    Drug misuse is not a disease, it is a decision, like the decision to step out in front of a moving car. [...] In this particular life-style the motto is "be happy now because tomorrow you are dying", but the dying begins almost at once, and the happiness is a memory.

  8. #8
    non è questa ???

    codice:
    #include<stdio.h>
     #include<conio.h>
     #include<string.h>
       struct studente {
    	char nome[];
    	char cognome[];
    	int  matricola;
    	} ;
     struct risultato {
    	char stringa [];
    	int  matricola;
    	};

  9. #9
    Utente di HTML.it L'avatar di pdpmpd
    Registrato dal
    Jan 2003
    Messaggi
    448
    se non usi il typedef devi ridichiarare ogni volta la sruct.
    altra cosa che avevo sotto il naso: stai passando intere strutture alle funzioni, non puntatori. in questo modo non puoi modificarle.
    le soluzioni sono due:
    o usi le variabili globali (+ semplice e immediato).
    o usi i puntatori.

    decidi te.
    Drug misuse is not a disease, it is a decision, like the decision to step out in front of a moving car. [...] In this particular life-style the motto is "be happy now because tomorrow you are dying", but the dying begins almost at once, and the happiness is a memory.

  10. #10
    grazie mille ti faro' sapere il voto

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.