Undeclared variable

Asked

Viewed 353 times

1

You’re making a mistake on lines 118,127,133,139...

127 20 'vector' was not declared in this Scope

#include<stdio.h>
#include <stdlib.h>
#include <string.h>
#define tamanho 5

 int pos=0;

struct agenda {

                char nome [40];
                char data_de_nascimento [10];
                char local_onde_mora [10];
                int telefone;
                char numero_matricula[80];
                char nome_pai [50];
                char nome_mae [50];
                char numero_identidade [50];
               }aluno;

int main();
int alterar(struct agenda vetor[]){

 char nomealteracao[100];
 int T=0; 
 int x;

 printf("Informe o nome do aluno a ser alterado:");
 gets(nomealteracao);
 for (x=0; x <= tamanho; x++){
  if(strcmp(vetor[x].nome,nomealteracao)==0){
 printf("Digite o novo nome:");
 gets(vetor[x].nome);
 printf("Data de  nascimento:");
 gets (vetor[x].data_de_nascimento);
 printf("Digite o novo numero da matricula:");
 gets(vetor[x].numero_matricula);
 printf("Digite o novo nome do pai:");
 gets(vetor[x].nome_pai);
 printf("Digite o novo nome da mae:");
 gets(vetor[x].nome_mae);
 printf("Digite o novo numero de identidade:");
 gets(vetor[x].numero_identidade);

   return T;


   }
}
if(T==1) {
 printf("Aluno nao cadastrado.");
 system("cls");
 return 0;
}

}

int  remover(struct agenda vetor[],int *Pos){
     char nome[40];
     int i,x;

     printf("Digite qual o nome do aluno que deseja remover:");
     gets(nome);

     for(i=0;i<=(*Pos);i++){
       if(strcmp (vetor[i].nome, nome)== 0){
          for(x = i + 1; x<= (*Pos); x++){
                vetor[x-1] = vetor[x];
          }
          (*Pos)--;

          break;
          system("cls");
       }
     } 
     main();

     if(i > tamanho){
          printf("Registro inxistente");
          system("cls");
    return 0;

     }
 int main();
}
int cadastrar ( struct agenda vetor[], int *pos){    

    if(*pos==tamanho){
                 main();

} else{
                     printf("Digite o nome: ");
                     gets(vetor[*pos].nome);
                     printf("Digite o numero da matricula: ");
                     gets(vetor[*pos].numero_matricula);
                     (*pos)++;

                     system("cls");

}
main();
}

int main(){

    int x;

    int opc;

    opc=0;


    if (opc== 0){
 printf ("Pesquisa do numero de matricula dos Alunos da Escola Estadual Joao Pereira:\n");
 printf("[1] - Cadastrar Aluno  \n[2] - Remover Aluno \n[3] - Alterar Informacoes \n[4] - Exibir Informacoes do Aluno \n[5] - Sair do Programa \n\n");
     printf("Digite sua opcao: ");
    scanf ("%d",&opc); fflush(stdin);
     system("cls");

  if(opc==1){
 cadastrar (vetor,&pos);
     pos++;
   main();
   system("cls");


    }
    if (opc ==2){

     opc = remover(vetor, &pos );

system("cls");
    }

    if (opc==3){
    opc = alterar(vetor);
    system("cls");
    main();
    }
    if (opc == 4 ){
      for(x = 0; x < pos; x++){
       printf("Nome: %s \n", vetor[x].nome);
       printf("Telefone: %d \n", vetor[x].matricula);

       system("cls"); 
    }
  main();
    }
    if (opc ==5){
     system("pause");
     return 0;
    }
    }


    getchar();

}
  • Take a look at [tour]. You can accept an answer if it solved your problem. You can vote on every post on the site as well. Did any help you more? You need something to be improved?

1 answer

2

Your code has several problems and as this is a very simple problem I imagine you are not understanding half of the code that is there. Solving this problem, you still won’t have the code working properly.

But to solve this problem you are saying it will be necessary to declare the variable vetor before using it as you did with other variables:

int main() {
    int x;
    int opc = 0;
    agenda vetor[50]; //para ter 50 cadastros no máximo
    ...

You need to better organize your code, it is very difficult to understand what is happening in it.

He’s using recursion. This is the last thing you want to do with the function main(). I will can create a memory problem. You need to replace this with a loop. Besides being much better is easier to work. When you have to declare this function before setting it you can already see that there is something wrong there.

You use many global variables. This is never good in any language.

Put a system("cls"); right after a printf() has the practical effect of doing nothing.

The function gets() has security problem and never should be used.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.