2
I was curious about the following code in c++.
int main(int argc, char** argv) {
float n1,n2,n3,n4;
int i = 1;
char nomeAluno;
printf("Digite seu nome: ");
scanf("%s", &nomeAluno);
while(i<5){
printf("Digite sua %dº nota: ", i);
scanf("%f", &n1);
i++;
}
printf("%.1f \n", n1);
printf("%.1f \n", n2);
printf("%.1f \n", n3);
printf("%.1f \n", n4);
return 0;
}
I have the following doubt, I don’t know if it’s possible, but if it’s how I can get inside the while the next note typing falls inside the N2? Because this way I did it keeps storing only in n1(Of course because of the loop).
The statement
char nomeAluno;
defines the variable that will contain one single character (in this case the format is %c) and not a string. For a string, which in C is an array of characters with the terminator character ' 0', usechar nomeAluno[40];
and in the scanf function do not put &. In fact in C++ you should use the string class. As for the notes you put the reading inside a loop and therefore every new reading you overwrite what was read in the previous reading. Either take separate readings for each note or use an array of notes (float nota[4];
).– anonimo
I didn’t notice the difference between using %c and %s over & didn’t know it was better to use a string. I’ll study more anyway and I’m grateful for the advice. :)
– Wenderson Beleli