我需要一点帮助。代码看起来很好。但是当我运行并输入一个非数字的数字时,它会无限地显示消息框错误,并且不会停止,除非我终止程序。有人可以告诉我一种解决方法吗?谢谢!无限消息框
CODE:
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <ctype.h>
#define pi 3.1416
int main()
{
float rad, area, dia, circ;
int radint;
char resp;
start:
system("cls");
printf("Chapter 1 \t\t\tProblem 1\n\n");
printf("Input the circle's radius: ");
radint = scanf("%f", &rad);
if(rad < 0)
{
MessageBox(0, "There cannot be negative measurements.", "Negative Value", 0);
goto start;
}
if(radint < 1)
{
MessageBox(0, "Non-numeric value is inputted.", "Character Value", 0);
goto start;
}
dia = rad * 2;
circ = 2 * pi * rad;
area = pi * rad * rad;
printf("\n\nDiameter = %0.2f | Circumference = %0.2f | Area = %0.2f\n\n", dia, circ, area);
printf("Try again? [Y/N]: ");
ret:
resp = getchar();
switch(resp)
{
case 'y': goto start;
case 'n': return;
default: goto ret;
}
}
附注:请不要'#define pi 3.1416'。即使Visual Studio应该有'',它定义了'M_PI'。 –
“isalpha()C Function faulty?” - ** No。** – 2014-01-25 10:42:14
找到一种不使用'goto','system(cls)'和特定于操作系统的东西(比如'MessageBox')来制作这个程序的方法,你的程序可能会变好10-100倍。在练习过程中,您可能也可以解决您的问题。 – Brandin