Ver Mensaje Individual
  #10 (permalink)  
Antiguo 17/12/2014, 13:56
Avatar de cursillosonline
cursillosonline
 
Fecha de Ingreso: diciembre-2014
Mensajes: 4
Antigüedad: 9 años, 4 meses
Puntos: 1
Respuesta: problemón con los punteros

Bueno, todo el código identado esta aquí.

Código C:
Ver original
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5. void AñadirAlumno(char **nombre, int *edad, int **notas, int numeroAlumnos);
  6. void MostrarAlumno(char **nombre, int *edad, int **notas, int numeroAlumnos);
  7. void GenerarFichero(char **nombre, int *edad, int **notas, int numeroAlumnos);
  8.  
  9. void main() {
  10.     char **nombre;
  11.     int *años;
  12.     int **notas;
  13.     int numeroAlumnos=0;
  14.     int fin=1;
  15.     int opcion;
  16.    
  17.     while(fin == 1) {
  18.         printf("1. Añadir alumno\n");
  19.         printf("2. Mostrar alumno\n");
  20.         printf("3. Generar Fichero\n");
  21.         printf("4. Salir\n");
  22.         printf("Elegir opcion\n");
  23.         fflush(stdin);
  24.         scanf("%i", &opcion);
  25.        
  26.         switch(opcion) {
  27.             case 1:
  28.                 printf("Introduce numero de alumnos:\n");
  29.                 fflush(stdin);
  30.                 scanf("%i", &numeroAlumnos);
  31.                 nombre = (char **)malloc(numeroAlumnos*sizeof(char*));
  32.                 años = (int *)malloc(numeroAlumnos*sizeof(int));
  33.                 notas = (int **)malloc(numeroAlumnos*sizeof(int *));
  34.                
  35.                 for(int i = 0; i < numeroAlumnos; i++) {
  36.                     nombre[i] = (char *)malloc(30*sizeof(char));
  37.                     notas[i] = (int *)malloc(3*sizeof(int));
  38.                 }
  39.                
  40.                 AñadirAlumno(nombre, años, notas, numeroAlumnos);
  41.                 break;
  42.             case 2:
  43.                 if(numeroAlumnos != 0) {
  44.                     MostrarAlumno(nombre, años, notas, numeroAlumnos);
  45.                 } else {
  46.                     printf("No existen alumnos\n");
  47.                 }
  48.                 break;
  49.             case 3:
  50.                 GenerarFichero(nombre, años, notas, numeroAlumnos);
  51.                 break;
  52.             case 4:
  53.                 fin = 0;
  54.                 break;
  55.             }
  56.            
  57.         free(años);
  58.            
  59.         for(int i = 0; i < numeroAlumnos; i++) {
  60.             free(nombres[i]);
  61.             free(notas[i]);
  62.         }
  63.            
  64.         free(nombres);
  65.         free(notas);
  66.     }
  67. }
  68.  
  69. void AñadirAlumno(char **nombre, int *edad, int **notas, int numeroAlumnos) {
  70.     for(int i = 0; i < numeroAlumnos; i++) {
  71.         printf("Introduce nombre\n");
  72.         fflush(stdin);
  73.         gets(nombre[i]);
  74.         printf("Introduce la edad\n");
  75.         fflush(stdin);
  76.         scanf("%i", &edad[i]);
  77.        
  78.         for(int j = 0; j < 3; j++){
  79.             printf("Introduce las nota %i", j+1);
  80.             fflush(stdin);
  81.             scanf("%i", &notas[i][j]);
  82.         }
  83. /*printf("Introduce las nota 2");
  84. fflush(stdin);
  85. scanf("%i", &notas[i][1]);
  86. printf("Introduce las nota 3");
  87. fflush(stdin);
  88. scanf("%i", &notas[i][2]);*/
  89.     }
  90. }
  91.  
  92. void MostrarAlumno(char **nombre, int *edad, int **notas, int numeroAlumnos) {
  93.     for(int i = 0; i < numeroAlumnos; i++) {
  94.         printf("Nombre: %s\n", nombre[i]);
  95.         printf("Edad: %i\n", edad[i]);
  96.         printf("Nota 1: %i Nota 2: %i Nota 3: %i\n", notas[i][0],notas[i][1],notas[i][2]);
  97.         }
  98. }
  99.  
  100. void GenerarFichero(char **nombre, int *edad, int **notas, int numeroAlumnos) {
  101.     FILE *fichero;
  102.     fichero = fopen("Texto.txt","w");
  103.    
  104.     if(fichero != NULL) {
  105.         for(int i = 0; i < numeroAlumnos; i++) {
  106.             fprintf(fichero, "Nombre: %s\n", nombre[i]);
  107.             fprintf(fichero, "Edad: %i\n", edad[i]);
  108.             fprintf(fichero, "Nota 1: %i Nota 2: %i Nota 3: %i\n", notas[i][0],notas[i][1],notas[i][2]);
  109.             fprintf(fichero, "Nota media: %f", ((float)notas[i][0]+(float)notas[i][1]+(float)notas[i][2])/3);
  110.             fprintf(fichero, "-------------------------------------------------");
  111.         }
  112.        
  113.     fclose(fichero);
  114.     } else {
  115.         printf("No se ha creado correctamente el fichero\n");
  116.     }
  117. }

Fallas que veo

1.- Uso de ñ.

Ni el lenguaje ni ningún compilador de C que conozca soporta la ñ.

2.- void main()

En algunos compiladores antiguos se permitia el retornar void en la función main, pero eso nunca a sido parte del estándar de C, asi que usa int main().

3.- nombres

Nunca definiste una variable declarada "nombres", remplazalo por "nombre".

4.- fflush(stdin)

El fflush fue hecho con la intención de limpiar el buffer de salida, no el de entrada, por lo que "fflush(stdin);" tiene un comportamiento indefinido.

5.- default:

Te falta el default del switch, en caso de que se ingrese una opción erronea.

Aparte de eso tienes un problema por la forma en que manejas tus arreglos que te genera un segmentation fault, la verdad me dio flojera ver ese tema, pero ya te di un empujón.

Última edición por cursillosonline; 17/12/2014 a las 14:11