【发布时间】:2016-03-12 10:04:41
【问题描述】:
printf("please enter a value for side a: ");
check1 = scanf(" %d*%c", &a);
while(check1 != 1)
{
printf("You have entered an invalid value");
scanf(" %d*%c", &a);
}
printf("The value for A is: %d\n", a);
我试图确保输入的值是一个整数,只有 (check1 != 1)。如果我输入除整数以外的任何内容并且 while 循环启动,它会无限打印“您输入的值无效”,但会忽略 scanf 以重新输入 A 的值。
我在此之前写了一段代码,它没有 (check != 1) 部分,但在 while 循环中有一个 scanf 有效。
printf("\nEnter the denominator number: ");
scanf("%d%*c", &num2);
while ( num2 <= 0 )
{
printf("The denominator can not be 0 or less, re enter the number: ");
scanf("%d%*c", &num2);
}
注意第二个代码块有效。
我该如何解决这个问题?或者有人可以提出一个简单的替代方法来确保“a”是一个整数?我对编程很陌生,scanf 是我知道的唯一输入提示。
非常感谢您的帮助:)
【问题讨论】: