0
I want to simulate a school database that collects name, math and physics grades and averages both for 5 students (defined in a string of struct
).
Any grade above 10 would be allowed, without any lock for it. Students would easily have grades above 10. So I thought I’d make a condition with if-else
and used the goto
, positioning its return at the beginning of the note request.
See the code below:
#include<stdio.h>
#include<locale.h>
int main(void){
setlocale(LC_ALL,"Portuguese");
struct Aluno{
char nome[10];
float notaMath, notaPhysics, media;
};
struct Aluno aluno[5];
int contador;
//Entrada de dados via teclado
for(contador = 0; contador < 5; contador++){
printf("Nome do aluno %d: ", contador+1);
scanf("%s",&aluno[contador].nome);
RETORNO1:
printf("Nota de matemática: ");
scanf("%f",&aluno[contador].notaMath);
if(aluno[contador].notaMath > 10.0){
printf("Apenas notas até 10.\n");
goto RETORNO1;
}
else{
goto POINT1;
}
POINT1:
RETORNO2:
printf("Nota de física: ");
scanf("%f",&aluno[contador].notaPhysics);
if(aluno[contador].notaPhysics > 10.0){
printf("Apenas notas até 10.\n");
goto RETORNO2;
}
else{
goto POINT2;
}
POINT2:
aluno[contador].media = (aluno[contador].notaMath + aluno[contador].notaPhysics)/2;
printf("\n");
}
printf("\n\n");
printf("------------Informações dos Alunos------------\n");
for(contador = 0; contador < 5; contador++){
printf("Nome do aluno %d: %s\n", contador+1, aluno[contador].nome);
printf("Nota de matemática: %.1f\n", aluno[contador].notaMath);
printf("Nota de física: %.1f\n", aluno[contador].notaPhysics);
printf("Média das notas: %.1f\n", aluno[contador].media);
printf("\n\n");
}
}
In this case, by placing any floating point values greater than 10, it prints "only notes up to 10" and actually returns to the set point, all ok. But if I put some floating point value, like 10.5, it goes into loop infinite.
Without the placement of goto
and the POINT
s and RETORNO
s he simply skips most of the following instructions and goes there forward in the loop for
.
Example of execution (data copied from .exe
):
"Nome do aluno 1: Jonathan
Nota de matemática: 10.5
Nota de física:
Nome do aluno 2: Nota de matemática:"
And why don’t you take that
goto
? I saw no advantage in him.– Maniero
You should take that There really has no advantage...
– Dev
I can even take it off, no problem. But, I wanted to know a way to limit the user to put notes up to 10, and make him return to the insertion of the same note, if he insists on putting a value greater than 10.
– Jonathan Álex
As far as I know, C forces me to use Else if I use If. If there is a way to use If alone, like in Java, teach me! haha
– Jonathan Álex
Yes you can remove the if and it is not necessary to put the non Else friend, because you do not have a counteroffer to condition..
– Dev
@Matheus you’re right. I tested it here and If worked alone. I remember from the beginning when I started with Dev Cpp, that he wouldn’t let If roll without Else. I’m using Codeblocks here and I’ve never tested this. It really was Dev’s trauma and lack of curiosity to test if it would work xD
– Jonathan Álex
Anyway, the Loop error continues :( Any alternative suggestion to limit the user and make him return to the insertion of the same note?
– Jonathan Álex
I just posted an answer.. See if you can sort of understand her
– Dev
Careful the way you’re using the
goto
. It’s not a good practice to use it that way. It gets very confusing for another person to understand doing these crazy jumps from one point of the code to the other as Voce did.– gfleck
Mandatory readings on
goto
: http://bioinfo.uib.es/~joemiro/Teach/material/escritura/gotoharmfulCol.pdf ; http://web.archive.org/web/20090320002214/http://www.ecn.Purdue.edu/Paramount/papers/rubin87goto.pdf– Jefferson Quesado
Thanks for the @gfleck touch. I’ve read a little about goto and that it’s not cool to use enough, but I couldn’t think of other alternatives. That’s why I came to you haha
– Jonathan Álex
Thanks for the reading suggestions, @Jeffersonquesado. I’ll read it all!
– Jonathan Álex
A life tip: Never use goto!
– Um Programador