0

I've been making a "contact List" where a get some informations like city, state, country, etc. But when after a get all those values from the user, the "cidade" field goes blank sometimes, if a just put characters in the other fields, "cidade" goes as it should be.

estruturas.cpp //My structures

typedef struct{
    char logradouro[100];
    char numero[5];
    char bairro[100];
    char cep[9];
    char cidade[100];
    char estado[100];
    char pais[100];
}endereco;



typedef struct{
    int codRegistro;
    char nome[100];
    char numero01[13];
    char numero02[13];
    endereco end;
}contato;

funcoes.cpp //My Functions

void verificarAberturaDeArquivo(FILE *file){
    if(file == NULL) {
        printf("Erro ao Abrir o Arquivo!");
    }
}



int numeroDeContatos(){ 
    int totalContatos;

    FILE *count = fopen("count.txt", "r+");
    verificarAberturaDeArquivo(count);

    fscanf(count, "%d", &totalContatos);
    fclose(count);
    return totalContatos;
}



void incrementoNumeroDeContatos(int totalContatos){

    FILE *count = fopen("count.txt", "w+");
    verificarAberturaDeArquivo(count);
    totalContatos++;
    fprintf(count, "%d", totalContatos);
    fclose(count);

}

// !Nao Pega o Nome da Cidade quando a informaçao é coletada junto a outros dados, somente é pega quando isolada.
void novoContato(FILE *agenda, contato *lista){
    printf("Nome: ");
    fflush(stdin);
    gets(lista -> nome);
    printf("Numero de Contato 01: ");
    fflush(stdin);
    gets(lista -> numero01);

    char op;
    do{
        printf("Deseja Adicionar um segundo numero para contato ?(y/n) ");
        fflush(stdin);
        op = getchar();
        if(op == 'y' || op == 'Y'){
            printf("Numero de Contato 02: ");
            fflush(stdin);
            gets(lista ->numero02);
        }
        else if(op == 'n' || op == 'N')
            break;
        else{
            printf("Insira um Valor Valido!!!\n");
        }
    }while(op != 'y' && op != 'Y' && op != 'n' && op != 'N');

    printf("Logradouro: ");
    fflush(stdin);
    gets(lista -> end.logradouro);

    printf("Pais: ");
    fflush(stdin);
    gets(lista -> end.pais);

    printf("Estado: ");
    fflush(stdin);
    gets(lista -> end.estado);

    printf("Cidade: ");
    fflush(stdin);
    gets(lista -> end.cidade);
    //printf("!!!!!!!!!!!!     %s", lista->end.cidade);

    printf("Bairro: ");
    fflush(stdin);
    gets(lista -> end.bairro);

    printf("Numero: ");
    fflush(stdin);
    gets(lista->end.numero);

    printf("CEP: ");
    fflush(stdin);
    gets(lista -> end.cep);

    int totalContatos = numeroDeContatos();
    lista->codRegistro = totalContatos + 1;
    incrementoNumeroDeContatos(totalContatos);

    //Salva os Dados Da estrutura no arquivo.
    agenda = fopen("agenda.txt", "a");
    verificarAberturaDeArquivo(agenda);

    fprintf(agenda, "%d\n" ,lista->codRegistro);
    fprintf(agenda, "%s\n", lista->nome);
    fprintf(agenda, "%s\n", lista->numero01);
    fprintf(agenda, "%s\n", lista->numero02);
    fprintf(agenda, "%s\n", lista->end.pais);
    fprintf(agenda, "%s\n", lista->end.estado);
    fprintf(agenda, "%s\n", lista->end.cidade);
    fprintf(agenda, "%s\n", lista->end.bairro);
    fprintf(agenda, "%s\n", lista->end.logradouro);
    fprintf(agenda, "%s\n", lista->end.numero);
    fprintf(agenda, "%s\n", lista->end.cep);

    fclose(agenda);
}

Main.cpp

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "estruturas.cpp"
#include "funcoes.cpp"


int main(){
    FILE *fp;

    contato lista;
    memset(&lista, 0, sizeof lista);

    novoContato(fp, &lista);
}

I'be been trying to solve this for almost one hour, and nothing...

aek8
  • 319
  • 1
  • 8
  • 22
  • 3
    Are you using C or C++? Your file types suggest C++ but the code looks light straight C code. – NathanOliver Feb 12 '18 at 17:13
  • 2
    Don't include source files, don't mix C and C++ tags. Decide on a language. – Ron Feb 12 '18 at 17:13
  • 3
    If you enter a 9-digit number (or name) in the `.cep` member, it will overwrite the start of the `.cidade` member (with a null byte). That's a buffer overflow, but would account for the symptom reported. – Jonathan Leffler Feb 12 '18 at 17:18
  • 1
    `fflush(stdin)` is UB. Never use `gets`, this function has been deprecated for a long time. Use `fgets` instead – Jabberwocky Feb 12 '18 at 17:19
  • @JonathanLeffler How could an assignment to .cep overwrite .cidade ? – Rafael Zeferino Rossi Feb 13 '18 at 12:42
  • @MichaelWalz in situations where the system gets my "enter" input and skips the next information input, what an other way to solve it without using fflush(stdin) ? – Rafael Zeferino Rossi Feb 13 '18 at 12:53
  • @RafaelRossi maybe it works on your platform, but techically it is UB. Otherwise you need to use platform specific functions. – Jabberwocky Feb 13 '18 at 13:00
  • The member `char cep[9];` can store a string of up to 8 bytes plus the null terminator. When you do `gets(lista->end.cep);`, if [`gets()` (bad idea)](https://stackoverflow.com/questions/1694036) reads more than 8 characters before the newline, it will write beyond the end of `cep`, and the next field is `cidade`. If the string is `"abcdefghi"`, the 9 alphabetic characters are copied to `cep` and the null byte overwrites `cidade[0]` — on most machines most of the time (there are weasel words in the standard that say it doesn't have to happen that way). You read `cidade` before `cep`. – Jonathan Leffler Feb 13 '18 at 18:09

0 Answers0