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.
I made the modifications and the options don’t work. The program stops working.
– Clarissa Galvao
@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.
– Lucas de Carvalho
In C there is no
===
. There is only=
for assignment and==
for comparison.– Victor Stafusa
Corrected by Victor!
– Lucas de Carvalho