我需要一點幫助。代碼看起來很好。但是當我運行並輸入一個非數字的數字時,它會無限地顯示消息框錯誤,並且不會停止,除非我終止程序。有人可以告訴我一種解決方法嗎?謝謝!無限消息框
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