I'm helping a friend with her programming class, and we've come across something funny. We have the code:
void ingresardatos(struct alumno *lista){
int i=0;
char continuar='s';
while(continuar=='s' && i<20){
printf("Valor de i al iniciar: %d \n", i);
printf("Introduzca el nombre del alumno:\n");
scanf("%s",&(lista[i].nombre));
printf("Introduzca la matricula:\n");
scanf("%s",&lista[i].matricula);
printf("Introduzca la primera calificacion:\n");
scanf("%f",&lista[i].calf1);
printf("Introduzca la segunda calificacion:\n");
scanf("%f",&lista[i].calf2);
printf("Introduzca la tercera caificacion:\n");
scanf("%f",&lista[i].calf3);
lista[i].prom=(lista[i].calf1+lista[i].calf2+lista[i].calf3)/3;
if(lista[i].prom<=5.9){
strcpy(lista[i].nota,"NA");
}
else if(lista[i].prom>=6 && lista[i].prom<=7.3){
strcpy(lista[i].nota,"S");
}
else if(lista[i].prom>=7.4 && lista[i].prom<=8.6){
strcpy(lista[i].nota,"B");
}
else if(lista[i].prom>=8.7 && lista[i].prom<=10){
strcpy(lista[i].nota,"MB");
}
printf("Valor de i antes: %d \n", i);
i++;
printf("Valor de i después: %d \n", i);
printf("¿Desea continuar? (S/N)");
scanf("%s",&continuar);
}
}
It's supposed to be a grade list for a class of 20; you introduce the student's data until you press "n", and it saves them on a list. Now, I figured out the pointer part (I just really know Java, so it's kinda weird working on C), but what I can't figure out is how to make the i++
part work. If you run it like it is, it'll start with i=0
on the first pass, then go through all the code, and finally do i++
before asking if you want to continue (it prints it on the screen). But then, when you press "s" to indicate you want to continue, it'll start with i=0
again, and for the life of me I can't figure out why. I tried i++
, ++i
, i=i+1
, and so on, but nothing seems to work. I even tried making i
a pointer, but Windows didn't like it and crashed my program every time I ran it.
If someone could explain just why it isn't working, I'd be eternally grateful.