How to make the program stop at each printf?

Asked

Viewed 123 times

0

I would like to make the user type a printf() at a time, being the "Name, RG and Email" but as soon as I type the name, it executes everything else and ends the program, as I make it stop in the next printf() for the user?

int main () {
    setlocale(LC_ALL, "Portuguese");

    int opcao, tipoPessoa, nomePessoa, rgPessoa, emailPessoa;

    printf("Bem Vindo ! \n O que deseja fazer ? \n 1 - Se cadastrar \n 2 - Reservar um lugar \n");
    scanf("%d",&opcao);

    if (opcao == 1){
        printf("Você é: \n 1 - Professor \n 2 - Aluno \n 3 - Convidado \n 4 - Portador de necessidade especial \n");
        scanf("%d", &tipoPessoa);
        switch(tipoPessoa){
            case 1: printf("Informe seu nome completo: ");
                    scanf("%d", &nomePessoa);
                    printf("Informe seu RG: ");
                    scanf("%d", &rgPessoa);
                    printf("Informe seu E-mail: ");
                    scanf("%d", &emailPessoa);

        }       
    }

}
  • First thing, why do you want everything to be a whole? The mistake could be this, because I see nothing that can cause the problem described, in fact, even so is to give this problem.

  • I’m actually a beginner in C, maybe that’s the problem ? what would be the solution ?

  • Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful for you. You can also vote on any question or answer you find useful on the entire site (when you have 15 points).

1 answer

2

When you need to take described data, i.e., text, you need to store it in strings, so you need to create an array of char with enough size for the amount of characters that will store 1 more characters that will be the terminator. Ideally you should limit data entry to this amount as per formatting of scanf(). The code is not yet ideal but for first exercises ok. I just think you should start learning more basic concepts, one thing at a time, in the right order, in a structured way to avoid generating more and more confusion in learning.

#include <stdio.h>

int main () {
    int opcao;
    printf("Bem Vindo ! \n O que deseja fazer ? \n 1 - Se cadastrar \n 2 - Reservar um lugar \n");
    scanf("%d", &opcao);
    if (opcao == 1) {
        int tipoPessoa;
        printf("Você é: \n 1 - Professor \n 2 - Aluno \n 3 - Convidado \n 4 - Portador de necessidade especial \n");
        scanf("%d", &tipoPessoa);
        switch(tipoPessoa) {
            case 1: {
                char nomePessoa[31], rgPessoa[10], emailPessoa[65];
                printf("Informe seu nome completo: ");
                scanf("%30s", nomePessoa);
                printf("Informe seu RG: ");
                scanf("%9s", rgPessoa);
                printf("Informe seu E-mail: ");
                scanf("%64s", emailPessoa);
            }
        }       
    }
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

  • 1

    are answers like yours that people who are getting need, thank you for responding the same way when I was learning too.

Browser other questions tagged

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