I’m trying to get a C on file, but the code won’t let me enter the address

Asked

Viewed 833 times

1

I’m trying to register in C, but the code does not let put the address and when I put the option to finalize the registration the program hangs and stops working. What am I doing wrong?

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

int main()
{
    char endereco, nome, cpf;
    int rg, cnh, codigo, opcao, dn;

    printf("Nome do Cliente: ");
    scanf("%s",&nome);

    printf("\nEndereco do Cliente: ");
    scanf("%s",&endereco);

    printf("\nNumero da Identidade (somente numeros):");
    scanf("%s",&rg);

    printf("\nNumero do CPF: ");
    scanf("%s",&cpf);

    printf("\nNumero da CNH (somente numeros): ");
    scanf("%s",&cnh);

    printf("\nCódigo do Cliente (somente numeros): ");
    scanf("%s",&codigo);

    printf("\nData de Nascimento (somente numeros): ");
    scanf("%s",&dn);

    printf("\nFinalizar Cadastro");
    printf("\n1 - Salvar");
    printf("\n2 - Cancelar\n");
    scanf("%s", opcao);
    if(opcao=1)
    {
        printf("Cadastro realizado com sucesso!");
    }
    else(opcao=2);
    {
        printf("Cadastro cancelado.");
    }
}

3 answers

1

if has to have 2 equal, ie if(opcao == 1). I only work with PHP, but I believe that in C it is also like this, just an equal sign =, means you’re adding something that, two signs of equal == is checking if it’s the same.

Another thing is that it has a dot and comma in its else I believe I’m also wrong.

Another thing wrong is that Else cannot have a condition, it is just Else (if not). You would have to use the else if

if(opcao == 1){
    printf("Cadastro realizado com sucesso!");
} else if(opcao == 2) {
    printf("Cadastro cancelado.");
}

Language C - Operators

  • I made the modifications and the options don’t work. The program stops working.

  • @Clarissagalo as I said, I only work with PHP, this part of if - If you fixed it, I believe that now is OK. Another part of the code I can’t tell you if it’s right or not, because I don’t know the C language very well.

  • In C there is no ===. There is only = for assignment and == for comparison.

  • Corrected by Victor!

0


First, = is an allocation operator, while == is for comparison. Therefore, you should use if (opcao == 1) instead of if(opcao=1).

In his else is missing a if after and has a ; further. As a result, the else(opcao=2);{ exchange the value of opcao to 2 and executes what is inside the { that follows. That’s not what you want. What you wanted is else if (opcao == 2) {.

Second, that you are confusing characters with strings. Instead of char endereco, nome, cpf;, should wear char endereco[50], nome[50], cpf[12];. The reason is that these fields are represented each by a string (string), not by an isolated character. The number that appears is the maximum string size. Remember that in C, all strings have a null terminator that consumes a character, and therefore it is important to leave space for it.

Another mistake is this:

scanf("%s", opcao);

What you wanted was this:

scanf("%d", opcao);

However, read anything with the scanf is a very devious thing to do. It’s very easy to make this horrific function suck.

An alternative is to use fgets To read the input, however, it is necessary to remove the line break from the end (a specific function is able to do this). However, this means that each string must have two more characters than its limit: one for the null terminator and the other for the line break that the fgets adds.

Use atoi to convert string to number.

Here’s your corrected code:

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

void removerNL(char *c) {
    c[strlen(c) - 1] = 0;
}

int main() {
    printf("Nome do Cliente: ");
    char nome[50];
    fgets(nome, 50, stdin);
    removerNL(nome);

    printf("\nEndereco do Cliente: ");
    char endereco[50];
    fgets(endereco, 50, stdin);
    removerNL(endereco);

    printf("\nNumero da Identidade (somente numeros):");
    char rg[20];
    fgets(rg, 20, stdin);
    removerNL(rg);

    printf("\nNumero do CPF: ");
    char cpf[13];
    fgets(cpf, 13, stdin);
    removerNL(cpf);

    printf("\nNumero da CNH (somente numeros): ");
    char cnh[20];
    fgets(cnh, 20, stdin);
    removerNL(cnh);

    printf("\nCódigo do Cliente (somente numeros): ");
    char scodigo[5];
    fgets(scodigo, 5, stdin);
    removerNL(scodigo);
    int codigo = atoi(scodigo);

    printf("\nData de Nascimento (somente numeros): ");
    char data[10];
    fgets(data, 10, stdin);
    removerNL(data);

    printf("\nFinalizar Cadastro");
    printf("\n1 - Salvar");
    printf("\n2 - Cancelar\n");

    char sopcao[5];
    fgets(sopcao, 5, stdin);
    removerNL(sopcao);
    int opcao = atoi(sopcao);

    if (opcao == 1) {
        printf("Cadastro realizado com sucesso!\n");
    } else if (opcao == 2) {
        printf("Cadastro cancelado.\n");
    } else {
        printf("Opcao desconhecida %d.\n", opcao);
    }

    printf("Nome: %s\n", nome);
    printf("Endereco: %s\n", endereco);
    printf("RG: %s\n", rg);
    printf("CPF: %s\n", cpf);
    printf("CNH: %s\n", cnh);
    printf("Codigo: %d\n", codigo);
    printf("Nascimento: %s\n", data);
}

Note that in the code above, I put a lot of printf at the end to show what it recorded. If you run this program with this entry:

Nome do Cliente: Vanessa da Silva
Endereco do Cliente: Rua do C++, 27
Numero da Identidade (somente numeros): 34467885-7
Numero do CPF: 25022198258
Numero da CNH (somente numeros): 6879326283
Código do Cliente (somente numeros): 34
Data de Nascimento (somente numeros): 10081973
1

Here’s the way out:

Finalizar Cadastro
1 - Salvar
2 - Cancelar
Cadastro realizado com sucesso!
Nome: Vanessa da Silva
Endereco: Rua do C++, 27
RG: 34467885-7
CPF: 25022198258
CNH: 6879326283
Codigo: 34
Nascimento: 10081973

See here working on ideone.

  • What is this for: void removerNL(char *c) { c[strlen(c) - 1] = 0; }

  • @Clarissagalvao To remove the line break that the fgets place at the end of the read string.

  • You are an incredible partner! Thanks!

  • @Clarissagalvao If this answer solved your problem and you have no more questions, mark it as accepted/correct by clicking on " " here on the side, which also marks your question as answered/solved. If on the other hand you are not yet satisfied, leave a comment and we will clarify.

0

You cannot register a CPF as an integer, even if it is without points, since it would return a large integer. You could even use an unsigned long int, but in case your system is 32-bit. A buffer overflow could happen. Use the friend’s tip above, because this is the best way to do your program. And always pay attention to the use of memory in C.

Browser other questions tagged

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