I was tasked to do a program to read the name of one student, 4 of their subjects and their respective grades. I have worked with for
and while
loops, also with if
statements and here is my code so far:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
main() {
printf("Este programa captura el nombre de un alumno \n");
printf ("y cuatro de sus materias y sus respectivas notas\n");
printf ("Nota: El programa solo toma en cuenta los dos primeros\n");
printf ("decimales de la notas expresada.\n\n");
char alumno[40] = {'\0'};
char mat[4][20] = {'\0', '\0', '\0', '\0'};
float calif[4] = {-1, -1, -1, -1};
int i;
while (alumno[0] == '\0') {
printf("Ingresa el nombre del alumno: ");
gets(alumno);
if (alumno[0] == '\0') {
printf("\nError. El alumno debe de llevar un nombre.");
printf ("\nTrata nuevamente\n\n");
};
};
for (i = 0; i < 4; ++i) {
while (mat[i][0] == '\0') {
printf("Ingresa el nombre de la materia %d: ", i+1);
gets(mat[i]);
if (mat[i][0] == '\0') {
printf("\nError. Las materias deben ser declaradas.");
printf ("\nTrata nuevamente.\n\n");
};
};
while (calif[i] < 0 || calif[i] > 10) {
printf("Ingrese la nota correspondiente a esta materia (0-10): ");
scanf("%2.2f", calif[i]);
if (calif[i] < 0 || calif[i] > 10) {
printf("\nError. Debe ingresar una nota válidad entre 0 y 10.");
printf ("\nTrata nuevamente.\n\n");
};
};
};
return 0;
};
The program seems to be working fine until it gets to the point to ask the grade of the first subject. Any grade I put it causes an infinite loop. I have searched about this problem to no avail. So please, let me know what I am doing wrong here.