Let’s start with this:
scanf("%d %d", &numero, &alturam);
This will then consume the two numbers at the beginning, but not the line-breaking that follows. The solution is to do this:
scanf("%d %d\n", &numero, &alturam);
Now, let’s see those two lines:
fflush(stdin);
scanf("%[^\n] %d", nome[i], &vet[i]);
The fflush(stdin);
going destroy any remaining entry into the programme. If you do this, your program will not be able to use the standard input given by the challenge for anything and anything else scanf
subsequent will fail. Don’t use that. Remember that in this challenge (actually in all URI challenges), the input is given all at once just at the start of the stdin
, and the existing data in the stdin
can only be read once because there is no way to rewind or rewind the stdin
.
Already the scanf("%[^\n] %d", nome[i], &vet[i]);
has a problem: The %[^\n]
will consume everything up to the line break, including the number to be read. When the %d
is tried, there will be no number available because the %[^\n]
will already have eaten it. And again, the line-breaking will continue there without being consumed as well.
So you’ll need something smarter to separate the number from the name:
scanf("%[^\n]\n", nome[i]);
char *final_do_nome = strrchr(nome[i], ' ');
sscanf(final_do_nome, " %d", &vet[i]);
final_do_nome[0] = '\0';
This way, the whole code looks like this:
#include <stdio.h>
int main(int argc, char** argv) {
int numero, alturam, i;
scanf("%d %d\n", &numero, &alturam);
int vet[numero];
char nome[numero][100];
for (i = 0; i < numero; i++) {
scanf("%[^\n]\n", nome[i]);
char *final_do_nome = strrchr(nome[i], ' ');
sscanf(final_do_nome, " %d", &vet[i]);
final_do_nome[0] = '\0';
}
for (i = 0; i < numero; i++) {
if (vet[i] > alturam) {
printf("%s\n", nome[i]);
}
}
return 0;
}
For this code, when given this entry:
3 50
Titan Colossal 60
Titan Encoracado 15
Titan Femea 14
This output is generated:
Titan Colossal
How to take it off, let’s try that other entrance:
8 50
Titan Colossal 60
Titan Encoracado 15
Titan Femea 14
Titan Supremo 90
Titan quase grande o suficiente 50
Titan grande o suficiente para passar por cima 51
Titanzao 70
Titanzinho 13
Produces this output:
Titan Colossal
Titan Supremo
Titan grande o suficiente para passar por cima
Titanzao
See here working on ideone.
Note at the end you store in %s, an int variable
– Victor