I'm trying to register in C, but the code does not let me put the address

1

I'm trying to register in C, but the code does not let the address and when I put the option to end the registration the program crashes 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.");
    }
}
    
asked by anonymous 30.10.2017 / 14:02

3 answers

0

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

On your else is missing if after and has ; more. As a result, else(opcao=2);{ changes the value from opcao to 2 and executes what is within the { that follows. This is not what you want. What you wanted is else if (opcao == 2) { .

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

Another error is this:

scanf("%s", opcao);

What you wanted was this:

scanf("%d", opcao);

However, reading anything with scanf is a very torturing thing to do. It's very easy to do this horrible job.

An alternative is to use fgets to read the entry, however it is necessary to withdraw the final line break (a specific function is able to do this). However, this means that each string must be two characters longer than its limit: one for the null terminator and another for the line break that fgets adds.

Use atoi to convert string to number.

Here is your patched 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 output:

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.

    
30.10.2017 / 15:17
1

The if must have 2 equals, or if(opcao == 1) . I only work with PHP, but I believe that in C is also like this, just an equal sign = , that is to say that you are adding something that, two equal signs == is checking if it is equal.

Another thing is that there is a semicolon in your%% of% I think it is wrong too.

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

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

Language C - Operators

    
30.10.2017 / 14:08
0

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

    
30.10.2017 / 15:42