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.
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!
skips the third scanf when you enter what value for the variable x?
– Sérgio S. Filho
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?
– Sérgio S. Filho
the scanf that fills the variable a, the third in the code, the program simply terminates or waits for an entry.
– Marlon Vinícius Buosi
Possible duplicate of Program does not read scanf
– Woss
Other Scanf is not stopping on repeat
– Woss