Error reading a string with scanf

1

The logic I already found, but I'm having trouble reading the string the way the question asks. It only accepts a number when I press enter, otherwise it considers the number one string.

Link to the question: link .

Here's my code:

#include <stdio.h>
int main(int argc, char** argv)
{
  int numero, alturam, i;
  scanf("%d %d", &numero, &alturam);
  int vet[numero];
  char nome[numero][100];
  for( i = 0; i < numero ; i++)
  {
    fflush(stdin);
    scanf("%[^\n] %d", nome[i], &vet[i]);
  }
  for( i = 0; i < numero ; i++)
  {
    if(vet[i] > alturam)
    {
        printf("%s\n", nome[i]);
    }
  }
return 0;
}
    
asked by anonymous 10.12.2017 / 15:50

1 answer

0

Let's start with this:

scanf("%d %d", &numero, &alturam);

This will consume the two numbers at the beginning, but not the following line break. The solution is to do this:

scanf("%d %d\n", &numero, &alturam);

Now, let's look at these two lines:

fflush(stdin);
scanf("%[^\n] %d", nome[i], &vet[i]);

The fflush(stdin); will destroy any remaining entries in the program. If you do this, your program will not be able to use the default input given by the challenge for nothing else, and any subsequent scanf will fail. Do not use this. Remember that in this challenge (actually in all URI challenges), input is given all at once at the start of execution in stdin , and data can not be read only once because you can not rewind or rewind stdin .

While stdin has a problem: scanf("%[^\n] %d", nome[i], &vet[i]); will consume everything until the line break, including the number to be read. When %[^\n] is attempted, there will be no available numbers because %d will already have eaten it. And again, the line-break 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] = '
#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] = '
3 50
Titan Colossal 60
Titan Encoracado 15
Titan Femea 14
'; } for (i = 0; i < numero; i++) { if (vet[i] > alturam) { printf("%s\n", nome[i]); } } return 0; }
';

In this way, the complete code looks like this:

Titan Colossal

For this code, when this entry is given:

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

This output is generated:

Titan Colossal
Titan Supremo
Titan grande o suficiente para passar por cima
Titanzao

As a catch-up, let's try this other entry:

scanf("%d %d", &numero, &alturam);

Produce this output:

scanf("%d %d\n", &numero, &alturam);

See here working on ideone.

    
10.12.2017 / 17:32