elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Usando Git para manipular el directorio de trabajo, el índice y commits (segunda parte)


  Mostrar Mensajes
Páginas: [1]
1  Programación / Programación C/C++ / Problema leer char * en: 1 Noviembre 2016, 05:55 am
Que tal, estoy haciendo un programa y me surge un problema, lo que pasa es que a la hora de leer mi variable char* no la esta aceptando.....
Código
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <ctype.h>
  4. #include <string.h>
  5.  
  6. struct Alumno{
  7.    char *nombre;
  8.    int matricula;
  9.    int id_Materias[4];
  10. };
  11.  
  12. int matricula = 1;
  13.  
  14. typedef struct Alumno ALUMNO;
  15.  
  16. int validate(char *opcion, int op); //Opcion 1 Valida digitos, Opcion 2 Valida chars
  17. void add_Alumno(ALUMNO *);
  18. void print_Alumno(ALUMNO *);
  19.  
  20. int main(){
  21.    ALUMNO alumno;
  22.    add_Alumno(&alumno);
  23.    print_Alumno(&alumno);
  24.    system("pause");
  25.    return 0;
  26. }
  27.  
  28. void add_Alumno(ALUMNO *alumno){
  29.    int pass = 0;
  30.    char *opcion;
  31.    int flag = 0;
  32.    int i = 0;
  33.    int cont_Materias = 0;
  34.    for(i=0; i<6; i++){
  35.        alumno->id_Materias[i] = 0;
  36.    }
  37.    do{
  38.        system("cls");
  39.        printf("\n\n\tIngrese el nombre del alumno: ");
  40.        fflush(stdin);
  41.        scanf(" %[^\n]", &(*opcion));
  42.        pass = validate(opcion, 2);
  43.    }while(pass != 1);
  44.    printf("flag1");
  45.    alumno->nombre = opcion;
  46.    printf("flag2");
  47.    system("cls");
  48.    printf("\n\n\tLa matricula del alumno es: %08d",matricula);
  49.    system("pause");
  50.    alumno->matricula = matricula;
  51.    system("cls");
  52.    do{
  53.        do{
  54.            do{
  55.                system("cls");
  56.                printf("\n\n\t***** MATERIAS *****");
  57.                printf("\n\n\t1. MATEMATICAS");
  58.                printf("\n\t2. ESPANIOL");
  59.                printf("\n\t3. PROGRAMACION");
  60.                printf("\n\t4. ARTES");
  61.                printf("\n\t5. CULTURA DE CALIDAD");
  62.                printf("\n\t6. CALCULO");
  63.                printf("\n\n\tIngrese el id de la materia: ");
  64.                scanf(" %[^\n]", &(*opcion));
  65.                pass = validate(opcion, 1);
  66.            }while(pass != 1);
  67.            pass = atoi(opcion);
  68.        }while(pass<1 || pass>6);
  69.        flag = 0;
  70.        for(i=0; i<6; i++){
  71.            printf("\n\tMateria: %d", alumno->id_Materias);
  72.            system("pause");
  73.            if(alumno->id_Materias[i] == pass){
  74.                flag = 1;
  75.                break;
  76.            }
  77.        }
  78.        if(flag != 1)
  79.            alumno->id_Materias[cont_Materias] = pass;
  80.    }while(flag != 1);
  81.  
  82.  
  83. }
  84.  
  85. void set_Name_Alumno(ALUMNO *alumno){
  86.    printf("\n\tSu nombre actual es: ");
  87.    print_Alumno(alumno);
  88.    system("pause");
  89.    int pass = 0;
  90.    char *opcion;
  91.    do{
  92.        system("cls");
  93.        printf("\n\tIngrese su nuevo nombre: ");
  94.        fflush(stdin);
  95.        scanf(" %[^\n]", &(*opcion));
  96.        pass = validate(opcion, 2);
  97.    }while(pass != 1);
  98.    alumno->nombre = opcion;
  99. }
  100.  
  101. void print_Alumno(ALUMNO *alumno){
  102.    printf("\n\tNombre: %s", alumno->nombre);
  103.    printf("\n\tMatricula: %08d", alumno->matricula);
  104. }
  105.  
  106. int validate(char *opcion, int op){
  107.    int i = 0;
  108.    for(i=0; i<strlen(opcion); i++){
  109.        if(!isdigit(opcion[i]) && op == 1){
  110.            return 0;
  111.        }
  112.        if(!isalpha(opcion[i]) && op == 2 && !(opcion[i] == ' ')){
  113.            return 0;
  114.        }
  115.    }
  116.    return 1;
  117. }


Mod: Los códigos deben ir en etiquetas GeSHi
Páginas: [1]
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines