The general rule is: don’t try to do anything complicated with scanf.
The purpose of the scanf function is to work with structured data, normally read from a file.
To work with interactive data reading you usually need to use a library, such as "ncurses" on Linux, or else analyze each line "manually", which is laborious.
I’ve never seen a program "in real life" that uses scanf, only in examples and school exercises. :)
Having said all this, you can do something, as in the example below, but I think it does not make up for the work, it will always end up being "half mouth". :)
#include<stdio.h>
#include<string.h>
#define N_ALUNOS 3
static void ignoreRestOfLine(void)
{
char c;
while((c = getchar()) != '\n' && c != EOF)
/* discard */ ;
}
int main()
{
int i;
float nota[N_ALUNOS];
char nome[15];
for (i = 0; i < N_ALUNOS; i++)
{
printf("Digite o nome do %d(o) aluno: ", i+1);
scanf("%14[ .a-zA-Z]%*[^\n]\n", nome);
printf("Digite a nota do aluno %s: ", nome);
scanf("%f", ¬a[i]);
ignoreRestOfLine();
printf("\n");
}
}
read this: http://answall.com/q/42981/101. This question is probably duplicated if not from some other, this problem is recurring.
– Maniero