Hola estoy empezando a programar en C y estoy con un ejercicio de calculo de promedio de unas calificaciones con while que a mi entender deberia funcionar pero no lo hace! Aqui esta mi codigo
Código:
//3.8 CLASS AVERAGE PROGRAM WITH SENTINEL CONTROLLED REPETITION
#include <stdio.h>
int main (void)
{
int counter;//number of grades entered
int grade;//grades value
int total;//sum of grades
float average;//average
//initialization phase
total = 0;
counter = 0;
grade = 0;
//processing phase
while( grade != -1) {
puts( "Please type the grade and press enter. To finish type \"-1\"." );//asks user to type a grade
scanf( "%d", &grade );//stores the grade typed by the user into the grade variable
total = total + grade;//adds the grade typed by the user to the total
counter = counter + 1;//adds one to the counter
}//end while
//termination phase
//if counter is not equal to zero, calculate the average and print it, if not print "No grades were entered"
if( counter != 0){
average = ( float ) total / counter;
printf( "Average is %.2f\n", average );
}//end if
else{
puts( "No grades were entered" );
}//end else
}//end main
y aqui esta el codigo que funciona, pero me gustaría entender por que el mio no lo hace:
Código:
//3.8 CLASS AVERAGE PROGRAM WITH SENTINEL CONTROLLED REPETITION
#include <stdio.h>
int main (void)
{
int counter;//number of grades entered
int grade;//grades value
int total;//sum of grades
float average;//average
//initialization phase
total = 0;
counter = 0;
//processing phase
puts( "Please type the grade and press enter. To finish type \"-1\"." );//asks user to type a grade
scanf( "%d", &grade );//stores the grade typed by the user into the grade variable
while( grade != -1) {
total = total + grade;//adds the grade typed by the user to the total
counter = counter + 1;//adds one to the counter
puts( "Please type the grade and press enter. To finish type \"-1\"." );//asks user to type a grade
scanf( "%d", &grade );//stores the grade typed by the user into the grade variable
}//end while
//termination phase
//if counter is not equal to zero, calculate the average and print it, if not print "No grades were entered"
if( counter != 0){
average = ( float ) total / counter;
printf( "Average is %.2f\n", average );
}//end if
else{
puts( "No grades were entered" );
}//end else
}//end main
Desde ya MUCHAS GRACIAS!