Error reading string with scanf

Asked

Viewed 650 times

1

The logic I already found, but I’m having a hard time reading the string the way the question asks. Only accepts a number when I hit enter, if it does not consider the number a string.

Link to the question: https://www.urionlinejudge.com.br/judge/pt/problems/view/2650.

Here is 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;
}
  • Note at the end you store in %s, an int variable

1 answer

0


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.

  • thank you very much.

  • @rafaelmarques If this answer solved your problem, mark it as accepted by clicking on the " " that is next to it. This will also mark your question as solved/solved. If you are still not satisfied and have any doubts or objections, feel free to comment.

Browser other questions tagged

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