Programa pula scanf

Asked

Viewed 162 times

-2

My program skips the third Scanf, I’ve tried several ways to make it work but I couldn’t, it seems to be some memory allocation problem, but I’m not sure, someone could help me?

#include <stdio.h>

int impres();

int main()
{
        int x, c, i, a;
        float matriculas[500];
        scanf (" %d", &x);
        for (c=0; c!=x; c++)
        {
                for (i=0; i<2; i++)
                {
                        scanf (" %f", &matriculas[c]);
                }
                i=0;
        }

        //impressão:

        scanf (" %d", &a);
        for (c=0; c<500; c++)
        {
                if (a==matriculas[c])
                {
                        printf ("achou mizeravi");
                        break;
                }
        }
        retrun 0;
}
  • skips the third scanf when you enter what value for the variable x?

  • or better, you mean third scanf, the third time the program expects a value input or scanf that fills variable a, which is the third one written in the code?

  • the scanf that fills the variable a, the third in the code, the program simply terminates or waits for an entry.

  • Possible duplicate of Program does not read scanf

1 answer

2

Good afternoon Marlon,

Your code presents some problems.

  • First problem: Lack of organization and clarity

For you who is building the program it is very clear what it should do, but for those who are not. What stores the variable "x"? and the "c"? These names don’t say anything to other people who read your code, so the first piece of advice I can give you is: organize your code as clearly as possible, it’s great practice.

  • Second problem: for

I couldn’t help but notice that your code is running on top of unnecessarily, and is applying in a very confusing way.

 for(c=0; c!=x; c++){
       for (i=0; i<2; i++){

       scanf("%f", &matriculas[c]);

      }
       i=0;
  }

In this section, every time for from the top happening, you will run the second for to save the values, but it asks for two entries, so the second will overwrite the first one. (At least that’s what I got, you understand, it’s pretty confusing)

Since the need is only save the number of enrollment respecting the amount of enrolled students you requested, why not apply ONE simple for?

for(i=0; i<qtdAlunos; i++){
    scanf("%f", &matriculas[i]);             
  }
  • Third problem: Different data types

Its variable that saves the searched registration is a integer, the vector that saves the number plates is of the type float. I don’t see much point in putting a number plate like float, But let’s assume it’s a necessity of your code.

If your license plate is "12345.0000" and you search for "12345", it will not give match.

Then another repair would be for variables of the same type, in case, float.

Final code:

#include <stdio.h>

#define QTD_MATRICULAS_MAX 500

int main(){

    int i, qtdAlunos;
    float matriculas[QTD_MATRICULAS_MAX], matriculaProcurada;

    scanf("%d", &qtdAlunos);

    for(i=0; i<qtdAlunos; i++){

    scanf("%f", &matriculas[i]);

    }

    scanf("%f", &matriculaProcurada);

    for (i=0; i<qtdAlunos; i++){

            if(matriculaProcurada == matriculas[i]){

            printf ("Matricula encontrada: %1.f", matriculas[i]);
            break;

      }else{

          printf("Matricula não encontrada!");
          break;
      }
   }
    return 0;
}

I hope I’ve helped!

Hugs and good studies!

Browser other questions tagged

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