Even using fflush, the fscanf function is not working

-1
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <locale.h>
struct cadastro{
    char nome[30];
    float notas[1][4];
    int faltas;
}alunos[3];

void LimpaTela(){//função para limpar a tela
    system("cls");
}

void incluir(struct cadastro inclusao[]){
    FILE *arq;

    printf("\tFaça o cadastro dos alunos...\n");
    arq = fopen("arquivo.txt", "w");
    fprintf(arq, "%s", "Nomes dos alunos...\n");
    int i, j, k;
    for(i=0;i<3;i++){
        printf("Inf. o nome: ");
        fflush(stdin);
        fgets(inclusao[i].nome, 30, stdin);
        fprintf(arq, "%s",inclusao[i].nome);
        for(j=0;j<1;j++){
            for(k=0;k<4;k++){
                printf("Inf. as notas: ");
                fflush(stdin);
                fscanf(arq, "%d" ,&inclusao[i].notas[j][k]);
                fprintf(arq, "%d",inclusao[i].notas[j][k]);
            }
        }
    }

    fclose(arq);    
}

int menu(){//funcao para o menu do trabalho
    int escolha;
    printf("1) Limpar o conteudo do arquivo TXT\n");
    printf("2) Incluir conteudo do arquivo TXT\n");
    printf("3) Sair\n");
    printf("O que deseja fazer? ");
    scanf("%d",&escolha);
    int *ptr=&escolha;
    return *ptr;
}

int main(){ 
    setlocale(LC_ALL, "Portuguese");
    int menu1, contador_saida=0;
    char ch;

    LimpaTela();
    menu1 = menu();
    switch(menu1){
        case 2:
            LimpaTela();
            incluir(alunos);
        break;
    }
    printf("\nSaindo...\n");
    system("pause");
    return 0;
}
    
asked by anonymous 10.11.2017 / 17:57

1 answer

0

You are probably waiting for fflush(stdin) to clear your input stream of any characters left over from the last entry, but fflush is meant to only clean output streams .

The behavior of fflush when parameter input is dependent on implementation. Some implementations will clean up an input stream as well, but this behavior is not portable and you should not trust it.

See this answer for a solution to your problem that does not involve fflush(stdin) .

    
10.11.2017 / 18:47