I’m having trouble printing a variable in my program

Asked

Viewed 59 times

0

I’m doing a program that simulates a bank, in function criarpessoafisica I’m trying to get the algorithm to check that Cpf is already registered, but it’s not working ! For some reason he printed the normal Cpf on that line

 printf("\ncpf %s\n",cb->cocom[numcontas].pessoas[num].cpf );

But not on that line;

printf("cpf %s ver %s i %i\n",cb->cocom[numcontas].pessoas[i].cpf, verificador, i  );

complete code;

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <windows.h>
#include <locale.h>
#include <math.h>
#include <conio.h>
#include <unistd.h>
//------------------------------------------------------------------------------
int num=0;
int numcontas=0;
int tipoconta=-1;
//------------------------------------------------------------------------------
typedef struct Pessoa{
  char nome [100];
  char endereco [100];
  char telefone [12];
  float renda;
  int tipopessoa; //1=fisica e 2=Juridica
  char cpf [20],cnpj [20];
}pe;

typedef struct Contacomum{
  char senha [5];
  char numconta [4];
  char agencia [4];
  float saldo; 
  pe pessoas [100];
  int pessoa;
}cc;
typedef struct contabancaria{
  cc cocom [20];
}cb;
//------------------------------------------------------------------------------
void gotoxy(int x, int y){
     SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),(COORD){x-1,y-1});
     //muda o cursor para a posição x,y
   }
//------------------------------------------------------------------------------
void fazerjanela (int ci,int li,int lf,int cf){
  //faz uma janela c=coluna l=linha i=incial f=final
  int i,y;
  for ( i = ci+1; i < cf; i++) {
    gotoxy(i,ci);printf("%c",196 );
  }
  for ( i = ci+1; i < cf; i++) {
    gotoxy(i,lf);printf("%c",196 );
  }
  for ( i = li; i < lf; i++) {
    gotoxy(ci,i);printf("%c",179 );
  }
  for ( i = li; i < lf; i++) {
    gotoxy(cf,i);printf("%c",179 );
  }
  gotoxy(ci,li);printf("%c",218 );
  gotoxy(cf,lf);printf("%c",217 );
  gotoxy(cf,li);printf("%c", 191);
  gotoxy(ci,lf);printf("%c", 192);
  gotoxy(cf,lf+1);printf("\n");
}
//------------------------------------------------------------------------------
void criarpessoafisica (cb *cb){
  int tam, flag=0, cout, i=0, j=0;
  char verificador [14];
  if (tipoconta == 0) {
    cb->cocom[numcontas].pessoas[num].tipopessoa = 1;
    system("cls");    fazerjanela(1,1,15,50);
    gotoxy (15,2); printf("Cadastro de Pessoa Fisica");
    gotoxy (3,4);   printf("Pesssoa N00%i ", num+1);
    gotoxy (3,6);   printf("Nome: ");fflush (stdin);
    scanf("%[^\n]s", &cb->cocom[numcontas].pessoas[num].nome);
    gotoxy (3,7);   printf("Endereco: ");fflush (stdin);
    scanf("%[^\n]s", &cb->cocom[numcontas].pessoas[num].endereco );
    gotoxy (3,8);   printf("Telefone: ");fflush (stdin);
    scanf("%[^\n]s", &cb->cocom[numcontas].pessoas[num].telefone );
    gotoxy (3,9);   printf("Renda Mensal: ");fflush (stdin);
    scanf("%f", &cb->cocom[numcontas].pessoas[num].renda );
    do {
      flag=0;
      gotoxy (3,12); printf("                                            ");
      gotoxy (3,14); printf("                                            ");
      gotoxy (3,10); printf("                                     ");
      gotoxy (3,10); printf("CPF: ");fflush (stdin);
      scanf("%s", &verificador );
      printf("\nverifi %s", verificador);
      system("cls");
      strcpy (cb->cocom[numcontas].pessoas[num].cpf,verificador);
      printf("\ncpf %s\n",cb->cocom[numcontas].pessoas[num].cpf ); sleep(1);
      for ( i = 0; i < num; i++) {
        cout=0;
        printf("cpf %s ver %s i %i\n",cb->cocom[numcontas].pessoas[i].cpf, verificador, i  );sleep(1);
        for ( j = 0; j < 11; j++) {
          if (verificador[j] == cb->cocom[numcontas].pessoas[i].cpf[j]) {
            cout++;printf("\ncout %i", cout);
            if (cout == 11) {
              flag=1;
            }
          }
        }
      }
      if (flag == 1) {
        gotoxy (3,12); printf("Error CPF já cadastrado !");
        gotoxy (3,14); system ("PAUSE");
      }
    } while(flag);
    gotoxy (3,12); printf("Pessoa cadastrada com sucesso !");
  }
}
//------------------------------------------------------------------------------
void cadastrarpessoa (cb *cb){
  int pos1=1;
  char opc;
  do{
      system("cls"); fazerjanela (1,1,15,50);
      gotoxy (6,5);  printf("Que tipo de pessoa deseja cadastrar ?");
      gotoxy (6,8);  printf("   Pessoa Fisica");
      gotoxy (25,8); printf("   Pessoa Juridica");
      gotoxy (40,14);printf("   Voltar");
      switch (pos1){
        case 1: gotoxy(6,8);    printf("-> Pessoa Fisica");break;
        case 2: gotoxy(25,8);   printf("-> Pessoa Juridica");break;
        case 3: gotoxy(40,14);  printf("-> Voltar");break;
      }
       opc=getch();
       if (opc==-32){ //tecla especial necessita pegar segundo valor
          opc=getch();
          switch (opc) {
            case 75:pos1--; break; //SETA PRA CIMA
            case 77:pos1++; break; //SETA PRA BAIXO
          }
       }
     if (pos1==0) pos1=3;
     if (pos1==4) pos1=1;
     if (opc == 13) //ENTER
       switch (pos1){
         case 1:
           criarpessoafisica (&cb);
           opc=27;
         break;
         case 2:
           opc=27;
         break;
         case 3:
                opc=27;
         break;
       }
   }
   while (opc!=27);
}
//------------------------------------------------------------------------------
void criarcontacomum(cb *cb){
  int pos1=1;
  char opc;
  tipoconta = 0;
  do{
    system ("cls");  fazerjanela(1,1,15,50);
    gotoxy (2,4);   printf("-----------------(Conta Comum)------------------");
    gotoxy (5,7);   printf("   Cadastrar Nova Pessoa ");
    gotoxy (5,9);   printf("   Usar Pessoa Existente ");
    gotoxy (2,12);  printf("------------------------------------------------");
    gotoxy (42,14); printf("   Sair");
    switch (pos1){
      case 1: gotoxy(5,7);  printf("-> Cadastrar Nova Pessoa ");           break;
      case 2: gotoxy(5,9); printf("-> Usar Pessoa Existente ");        break;
      case 3: gotoxy(42,14);printf("-> Sair");                  break;
    }
     opc=getch();
     if (opc==-32){ //tecla especial necessita pegar segundo valor
        opc=getch();
        switch (opc) {
          case 72:pos1--; break; //SETA PRA CIMA
          case 80:pos1++; break; //SETA PRA BAIXO
        }
     }
   if (pos1==0) pos1=3;
   if (pos1==4) pos1=1;
   if (opc == 13) //ENTER
       switch (pos1){
         case 1:
          cadastrarpessoa (&cb);
          //gerarconta (&cb);
          num++;  numcontas ++;
         break;
         case 2:

         break;
         case 3:
                opc=27;
         break;
       }
   }
   while (opc!=27);
 }
//------------------------------------------------------------------------------
void criarconta(cb *cb) {
  int pos1=1;
  char opc ;
  do{
      system ("cls");  fazerjanela(1,1,15,50);
      gotoxy (2,4);   printf("--------------------(Conta)---------------------");
      gotoxy (5,6);   printf("   Conta Comum ");
      gotoxy (5,8);   printf("   Conta Especial ");
      gotoxy (5,10);   printf("   Conta Poupanca ");
      gotoxy (2,12);  printf("------------------------------------------------");
      gotoxy (42,14); printf("   Sair");
      switch (pos1){
        case 1: gotoxy(5,6);  printf("-> Conta Comum");           break;
        case 2: gotoxy(5,8);  printf("-> Conta Especial");        break;
        case 3: gotoxy(5,10);  printf("-> Conta Poupanca" );       break;
        case 4: gotoxy(42,14);printf("-> Sair");                  break;
      }
       opc=getch();
       if (opc==-32){ //tecla especial necessita pegar segundo valor
          opc=getch();
          switch (opc) {
            case 72:pos1--; break; //SETA PRA CIMA
            case 80:pos1++; break; //SETA PRA BAIXO
          }
       }
     if (pos1==0) pos1=4;
     if (pos1==5) pos1=1;
     if (opc == 13) //ENTER
       switch (pos1){
         case 1:
          criarcontacomum (&cb);
         break;
         case 2:
          //criarcontaespecial (&cb);
         break;
         case 3:
         //criarcontapoupanca (&cb);
         case 4:
          opc=27;
         break;
       }
   }
   while (opc!=27);
}
//------------------------------------------------------------------------------
int main() {
  system("mode con:cols=50 lines=17");
  cb contabancaria;
  int pos1=1;
  char opc;
  //lerarquivo (&contabancaria); //arumar <<<<<<<<<<<<<<<<<<<<<<<<<<<<<------
  do{
      system("cls"); fazerjanela (1,1,15,50);
      gotoxy (2,4);   printf("--------------------(Conta)---------------------");
      gotoxy (5,8);   printf("   Criar ");
      gotoxy (31,8);  printf("   Acessar ");
      gotoxy (2,12);  printf("------------------------------------------------");
      gotoxy (42,14); printf("   Sair");
      switch (pos1){
        case 1: gotoxy(5,8);  printf("-> Criar");       break;
        case 2: gotoxy(31,8); printf("-> Acessar");       break;
        case 3: gotoxy(42,14);printf("-> Sair");           break;
      }
       opc=getch();
       if (opc==-32){ //tecla especial necessita pegar segundo valor
          opc=getch();
          switch (opc) {
            case 75:pos1--; break; //SETA PRA ESQUERDA
            case 77:pos1++; break; //SETA PRA DIREITA
          }
       }
     if (pos1==0) pos1=3;
     if (pos1==4) pos1=1;
     if (opc == 13) //ENTER
       switch (pos1){
         case 1:
          criarconta(&contabancaria);
         break;
         case 2:
          //acessarconta(&contabancaria);
         break;
         case 3:
          opc=27;
         break;
       }
   }
   while (opc!=27);
   system("cls");
   printf("\nPrograma Finalizado com suscesso !\n");
}
  • if code is hard to read, all together, no blank lines separating anything...apparently you are not incrementing the variable "numcontas", so all your data entry is done only in index account 0

  • I am, after this function has a line "numcontas++;", but the problem is that it is not showing cb->cocom[numcontas].pessoas[i].cpf

1 answer

0

Just looking at the code you posted is not possible to know why it does not work the way you want it, probably have logic error in the part that was not shown.

But it is possible to make some considerations:

1 the correct format to read to the end of the line is "%[ n]", without the "s" after the bracket closes; in practice this normally don’t cause problems, but it’s good to keep an eye out

2 The correct way to consume the " n" after a scanf("%[ n]") is not through "fflush(stdin);"...this is not standard, it is an extension of Visual C++ that does not work on Linux, for example...the correct way is to put a space at the beginning of the format in the next scanf that is made, like this: scanf("%[ n]");... scanf(" %[ n]")

3 at all times it is necessary to check the scanf return, to check if there was no error in the data entry, thus:

n = scanf("%d%d", &x, &y);
if (n != 2)
{
  // tratar erro na entrada de dados!
  ....
}

That’s the possible thing to say.

UPDATING

As the program is a little big, and I can not compile (it was made for Windows, does not compile on Linux) it is difficult to discover something, but I found strange these lines

 cadastrarpessoa (&cb);
 //gerarconta (&cb);
 num++;  numcontas ++;  // <--------------

The superficial impression I have is that for account 0 is being created person 0, for personal 1 is being created person 1 (without creating person 0), and so on.

As I said above, I think it is a logic error, but it is difficult to say where, would have to analyze the entire program. It does not seem to be a C error.

  • I get it, but the problem is that it reads the variable correctly, but inside the for it doesn’t show the same thing it shows before the for. I got my full code working in case you want to take a look

Browser other questions tagged

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