Ir para conteúdo
Fórum Script Brasil
  • 0

(Resolvido) Código com erro!


fcojj

Pergunta

//bibliotecas usadas
#include <stdio.h>
#include <conio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>

//declação de variáveis globais


//main
void main (void) {
int num_cad=0, cod_aluno, q,i, j,auxiliar, x, c=0, y=50, codigo[num_cad],idade[num_cad],a;
char enc = 'v',continua, vai[10], esc, esc_2,nome_aluno[50], temporaria[50], nome[num_cad][100], city_natal[num_cad][100], city_mora[num_cad][100];
                    //do1      
     do{
                    printf("|*******************************************************|\n");
          printf("| Menu:                                                 |\n");
          printf("| 1 - Cadastro                                          |\n");
          printf("| 2 - Pesquisa                                          |\n");
          printf("| 3 - Relatorio                                         |\n");
          printf("| 4 - Sair                                              |\n");
          printf("|*******************************************************|\n");
                                        
                                 scanf("%d",&esc); //escolha do usuario
                               system("cls"); 
                                            }while ((esc!=1)&&(esc!=2)&&(esc!=3)&&(esc!=4)); //fechando do1
                                   
                    //while1                    
                    while(esc!=4){
                         //switch1
                                        switch(esc){
                                            
                                            case 1:
                                        
                    system("cls");
          printf("|*******************************************************|\n");
          printf("|1 - CADASTRO                                           |\n");
          printf("|*******************************************************|\n");
                                    
                                     
                 printf("DIGITE O NUMERO DE CADASTROS:\n");   
         scanf("%d",&a);
         num_cad+=a;  
                         
                        for(q=(num_cad-a);q<num_cad;q++){
                fflush(stdin);
                           printf ("\nNOME DO ALUNO:"); 
                             fgets(nome[q],100,stdin);
                             nome[q][0]=toupper(nome[q][0]);
                           
               printf ("CODIGO DO ALUNO:"); 
                             scanf ("%d", &codigo[q]);
                                                                                         
               printf ("IDADE DO ALUNO:");
               scanf("%d", &idade[q]);
               
               fflush(stdin);
              
                           printf ("CIDADE NATAL DO ALUNO:");
               fgets(city_natal[q],100,stdin);
               city_natal[q][0]=toupper(city_natal[q][0]);
               
                           printf ("CIDADE ONDE MORA O ALUNO :"); 
               fgets(city_mora[q],100,stdin);
                           city_mora[q][0]=toupper(city_mora[q][0]);}//for        
                           break;//case1                            
               
                                   case 2:
                                    //do2    
                                 do{        
                                        system("cls");
            printf("|*******************************************************|\n");
                 printf("|2 - PESQUISA:                                          |\n");
                 printf("|C - PESQUISA POR CODIGO                                |\n");
                 printf("|N - PESQUISA POR NOME                                  |\n");
              printf("|*******************************************************|\n");
                                         
                                        scanf("%c",&esc_2);
                                        esc_2 = toupper(esc_2);
                                      getchar(); } while((esc_2 != 'C')&&( esc_2 != 'N'));//fechando do2
                                     
                   //switch2
                                        switch(esc_2){
                                                                 
                                            case 'C':  
                                                                                         
                                            do{ 
                         system("cls");                     
                         i=0;
                         enc='v';     
                         printf("CODIGO DO ALUNO A SER PESQUISADO:");
                         scanf("%d",&cod_aluno);       
                         
                                                                                                    while ((i <= num_cad)&&(enc == 'v')){
                              if (codigo[i] == cod_aluno) 
                                                                                                                            enc = 'f';
                              else i++; }    
                                                                                                                           
                         if ( enc == 'v' ) 
                                                                                           printf("ALUNO NÃO CADASTRADO!");
                         else{
                           printf("O codigo do aluno pedido(%d) é de %s de %d anos", codigo[i],nome[i], idade[i]);
                           printf("\nEste aluno nasceu em %s e mora em %s", city_natal[i],city_mora[i]);
                           } 
                                                                                                                               
                         do{
                            printf("\nPROCURAR OUTRO ALUNO?(S/N)");
                            scanf("%c",&continua);        
                         }while((toupper(continua) != 'S') && (toupper(continua) != 'N'));
                      }while(toupper(continua) == 'S');
                      break;//case C
                                                                                    
                 case 'N':  
                                                                                        
                                                                                        do{
                           system("cls");
                           i=0;
                           enc='v';             
                           fflush(stdin);
                                                                                                            
                                                                                                            printf("NOME DO ALUNO A SER PESQUISADO:");
                           fgets(nome_aluno,100,stdin);
                           nome_aluno[0]=toupper(nome_aluno[0]);
       
                           while ((i<=num_cad)&&(enc=='v')){
                              if ( strcmp(nome[i], nome_aluno) == 0 ) 
                                                                                                                            enc='f';
                              else i++;}//while                       
       
                           if ( enc == 'v' ) 
                                                                                                               printf("ALUNO NÃO CADASTRADO!");
                           else{
                                printf("O aluno pedido (%s) tem %d anos", nome[i], idade[i]);
                                printf("\nEste aluno nasceu em %s e mora em %s", city_natal[i],city_mora[i]);
                             } 
                                                                                                                                      
                           do{
                              printf("\nPROCURAR OUTRO ALUNO?(S/N)");
                              scanf("%c",&continua);        
                         }while((toupper(continua) != 'S') && (toupper(continua) != 'N'));
                      }while(toupper(continua) == 'S');                            
                       break;
                       }//fechando switch2 
                                                                                     
                                    break;//case2
                                                
                                                                                                
                                        
                                         case 3:
                                        
                                     do{
                                     system("cls");
          printf("|*******************************************************|\n");
             printf("|3 - RELATORIO:                                         |\n");
             printf("|C - RLATORIO POR CODIGO                                |\n");
             printf("|N - RELATORIO POR NOME                                 |\n");
          printf("|*******************************************************|\n");
          getchar();
                                        
                                        scanf("%c",&esc_2);
                                     getchar(); } while((esc_2!= 'C') && ( esc_2 != 'N'));
                                     //switch3
                                        switch(esc_2){
                                           
                                                      case 'C':
                                                                                
                                                                                for( i = 0; i < num_cad-1; i ++ ){
                        for( j = i+1; j < num_cad; j ++ ){
                             if ( codigo[i] > codigo[j] ){
                                     auxiliar = codigo[i];
                                     codigo[i] = codigo[j];
                                     codigo[j] = auxiliar;
                                     auxiliar = idade[i];
                                     idade[i] = idade[j];
                                     idade[j] = auxiliar;
                                     strcpy(temporaria, nome[i]);
                                     strcpy(nome[i], nome[j]);
                                     strcpy(nome[j], temporaria);
                                     strcpy(temporaria, city_natal[i]);
                                     strcpy(city_natal[i], city_natal[j]);
                                     strcpy(city_natal[j], temporaria);
                                     strcpy(temporaria, city_mora[i]);
                                     strcpy(city_mora[i], city_mora[j]);
                                     strcpy(city_mora[j], temporaria);                             
                                     }
                                  }
                                                                                                                    }
                                                                        
                                                                        for(i=0;i<num_cad;i++){
                      printf("%d,%s,%d anos,%s,%s\n", codigo[i],nome[i],idade[i],city_natal[i],city_mora[i]);}
                       break;//case C
              
                                                         case 'N':
                                                                                
                                                                                for( i = 0; i < num_cad - 1; i ++ ){
                        for( j = i+1; j < num_cad; j ++ ){
                             if ( strcmp(nome[i],nome[j])>0 ){
                                     auxiliar = codigo[i];
                                     codigo[i] = codigo[j];
                                     codigo[j] = auxiliar;
                                     auxiliar = idade[i];
                                     idade[i] = idade[j];
                                     idade[j] = auxiliar;
                                     strcpy(temporaria, nome[i]);
                                     strcpy(nome[i], nome[j]);
                                     strcpy(nome[j], temporaria);
                                     strcpy(temporaria, city_natal[i]);
                                     strcpy(city_natal[i], city_natal[j]);
                                     strcpy(city_natal[j], temporaria);
                                     strcpy(temporaria, city_mora[i]);
                                     strcpy(city_mora[i], city_mora[j]);
                                     strcpy(city_mora[j], temporaria);                             
                                     }
                                  }
                             }
                            
                                                                                                                 for(i=0;i<num_cad;i++){
                                 printf("%d,%s,%d anos,%s,%s\n", codigo[i],nome[i],idade[i],city_natal[i],city_mora[i]); }
                                                                                                                                    break;//case N                           
                             }//switch2
                             
                                                                                                                    break;//case3
                                                          
                                                                                                                    
                                                
                                                                                                         
}//fechando switch1
                                                                                                    
          printf("|*******************************************************|\n");
          printf("| Menu:                                                 |\n");
          printf("| 1 - Cadastro                                          |\n");
          printf("| 2 - Relatorio                                         |\n");
          printf("| 3 - Pesquisa                                          |\n");
          printf("| 4 - Sair                                              |\n");
          printf("|*******************************************************|\n");
                                        
                                     scanf("%d",&esc); //escolha do usuario
                                     system("cls");        
                                 
}//while                                                                                
                                                                                                            
                                                                                                            
                                                                                                            
                                                                                                
                        printf("OBRIGADO POR USAR O NOSSO PROGRAMA!\n");

                        system("pause");

} //main

POR FAVOR ME AJUDE

Editado por Mônica Bonna
Adicionar tag CODE
Link para o comentário
Compartilhar em outros sites

6 respostass a esta questão

Posts Recomendados

  • 0

O problema dele é bem simples. Ele esta armazenando todos os dados em matrizes.

E esta inicializando elas errado.

Olhem o trecho do codigo

int num_cad=0, cod_aluno, q,i, j,auxiliar, x, c=0, y=50, codigo[num_cad],idade[num_cad],a;

Ele inicializa o num_cad=0 e em seguida declara duas matrizes de tamanho 0, que são idade e codigo. Para que ele corrija o problema ele devera determinar um tamanho maximo de cadastros. Caso ele queria trabalhar com alocação dinamica tera que usar ponteiros.

Estou certo Kuroi?

Link para o comentário
Compartilhar em outros sites

Participe da discussão

Você pode postar agora e se registrar depois. Se você já tem uma conta, acesse agora para postar com sua conta.

Visitante
Responder esta pergunta...

×   Você colou conteúdo com formatação.   Remover formatação

  Apenas 75 emoticons são permitidos.

×   Seu link foi incorporado automaticamente.   Exibir como um link em vez disso

×   Seu conteúdo anterior foi restaurado.   Limpar Editor

×   Você não pode colar imagens diretamente. Carregar ou inserir imagens do URL.



  • Estatísticas dos Fóruns

    • Tópicos
      152,3k
    • Posts
      652,3k
×
×
  • Criar Novo...