Help to solve this BUG, I am beginner. C Language

Asked

Viewed 61 times

-2

Preciso que estas perguntas estejam separadas

Follow code in full, below. I’m quite beginner yet, so there may be more mistakes.

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

//Var
char nome [200];
char cpf [200];
char sexo[2];
int idade;
int main();




struct elemento {
    char pergunta[256];
    int pontuacao;
};

struct elemento questoes[] = {
    { .pergunta = "Tem febre: ", .pontuacao = 5},
    { .pergunta = "Tem dor de cabeça: ", .pontuacao = 1},
    { .pergunta = "Tem secreção nasal ou espirros: ", .pontuacao = 1},
    { .pergunta = "Tem dor/irritação na garganta: ", .pontuacao = 1},
    { .pergunta = "Tem tosse seca: ", .pontuacao = 3},
    { .pergunta = "Tem dificuldade respiratória: ", .pontuacao = 10},
    { .pergunta = "Tem dores no corpo: ", .pontuacao = 1},
    { .pergunta = "Tem diarréia: ", .pontuacao = 1},
    { .pergunta = "Esteve em contato, nos últimos 14 dias, com um caso diagnosticado com COVID-19: ", .pontuacao = 10},
    { .pergunta = "Esteve em locais com grande aglomeração: ", .pontuacao = 3},
};



void perguntas(){
  
    char res;
    int soma = 0;
    int i = 0;
    

    FILE *arquivoTxt;
    arquivoTxt = fopen("database.txt", "a");
    
    if (arquivoTxt==NULL)
    {
        printf("Erro ao criar arquivo");
    }     
    
    system("@cls || clear");
    printf("Responda com S(sim) ou N(nao)\n");
    printf("\n");
    for(i = 0; i < 10; i++){
        printf("%s", questoes[i].pergunta);
        scanf("%c", &res);
        fflush(stdin);
        fprintf(arquivoTxt, "%s %c\n", questoes[i].pergunta, res);
        if ( res == 'S' || res == 's'){
            soma = soma + questoes[i].pontuacao;
        }
    }
    fprintf(arquivoTxt, "Pontuação Total: %d", soma);
    fclose(arquivoTxt);

    system("@cls || clear");
    if(soma >=0 && soma <10){
        printf("\nVocê somou %d pontos, vá para a ala de Baixo Risco\n", soma);
    }
    if(soma >=10 && soma <20){
        printf("\nVocê somou %d pontos, vá para a ala de Médio Risco\n", soma);
    }
    if(soma >=20){
        printf("\nVocê somou %d pontos, vá para a ala de Alto Risco\n", soma);
    }
    soma = 0;

    printf("\nPressione ENTER para voltar ao Menu Principal...");
    getchar();
    main();

}


void cadastrarPaciente(){
         
    setlocale(LC_ALL, "portuguese");
         
    FILE *arquivoTxt;
    arquivoTxt = fopen("database.txt", "a");
         
    if (arquivoTxt==NULL)
    {
        printf("Erro ao criar arquivo");
    }
    
    printf("\nInforme o nome do Paciente: ");
    scanf("%s", nome);
    fflush(stdin);
    
    printf("\nInforme o CPF do Paciente: ");
    scanf("%s", cpf);
    fflush(stdin);
    
    printf("\nInforme o Sexo do Paciente (M/F): ");
    scanf("%s", cpf);
    fflush(stdin);
    
    printf("\nInforme a idade do Paciente: ");
    scanf("%d", &idade);
    fflush(stdin);
    
    fprintf(arquivoTxt, "Nome: %s\nCPF: %s\nSexo: %s\nIdade: %d\n", nome, cpf, sexo, idade);
    fclose(arquivoTxt);
    
    printf("Dados gravados com sucesso\n");
    getchar();
    perguntas();


}

int main () {

    int opcao;

    printf("          -MENU-          \n\n");
    printf(" 1-   Cadastrar Paciente  \n");
    printf(" 0-        Sair           \n");
    printf("     Selecione uma opção  \n");
    scanf("%i", &opcao);
    fflush(stdin);

        switch(opcao) {
            case 0:
                printf("\nFinalizando Aplicação...");
                break;
            case 1:
                cadastrarPaciente();
                break;
            default:
                break;
        }
}
  • Hello William, welcome to the site. To proceed with the question it is important you [Dit] the question and add a [mcve] of the problem. To understand what kind of question serves the site and consequently avoid closures worth reading What is the Stack Overflow and the Stack Overflow Survival Guide (summarized) in Portuguese.

  • Do not use C and C++ tags at the same time when it is not really the case...

  • @Guilhermenascimento this is a minimal, compileable and verifiable example. The author included an example of the execution where the error is clear, and the title may not be very revealing but it is the expression of the case. 'Cause issues like that can be considered fora de escopo? Of course, it may not have been you who identified her like this and I apologize if that wasn’t the case

  • 1

    @arfneto the comment field is not local to debate motives, or how it works the closures and nor for you to seek to understand the affirmation an opinion based on assumptions, this should be done in the META: https://pt.meta.stackoverflow.com/. Anyway the code presented is not a [mcve], the problems should be specific and not something we need to analyze in the middle of a complete code a specific problem. I recommend that you also read the links, the existing META topics to have a fruitful participation for others and yourself.

No answers

Browser other questions tagged

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