以下程序提示输入计算圆柱体的体积。当输入任何数量的非数字事物时如何终止该程序?
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
float areaCirc(float r) {
return (M_PI*r*r);
}
float volCyl(float r, float h) {
return (areaCirc(r)*h);
}
int main(void) {
float r, h;
int k = 0;
float volume;
float avgh = 0;
float toth = 0;
do {
scanf("%f%f",&r,&h);
if(r<=0 || h<=0) {
break;
}
volume = volCyl(r,h);
k = k++;
printf(" Cylinder %d radius %.2f height %.2f volume %.2f\n",k,r,h,volume);
toth = toth + h;
} while(r>0 && h>0);
avgh = toth/k;
printf("Total Height: %.2f\n",toth);
printf("Average Height: %.2f\n",avgh);
return EXIT_SUCCESS;
}