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.