求 c 对 scanf 输入检查判断
/*
输入的整数是奇数还是偶数呢
*/
#include <stdio.h>
int main(void)
{
int cont; /* 要继续嗎 */
do {
int *no=(int *)malloc(10); // 原句是 int no;
printf("请输入一个整数: ");
if(scanf("%[a-z]d",&no)) // 原句是 scanf("%d",&no);
puts("error");
else
if (no%2)
puts("这个整数是奇数。");
else
puts("这个整数是偶数。");
printf("要重复一次吗? 【yes...0 / no ... 9]】:");
scanf("%d",&cont);
} while (cont == 0);
return (0);
}
执行出错在,if(no%2)…
cannot use pointers here.
另请问以下一句是什么意思呢?
int *no=(int *)malloc(10);
求解题,谢.
PS. 上述代码,用原句執行,
如输入非数字,则会出错。
[解决办法]
#include <stdio.h>
#include <malloc.h>
int main(void)
{
int cont; /* 要继续嗎 */
do {
int *no=(int *)malloc(10); // 原句是 int no;
printf("请输入一个整数: ");
if(scanf("%[a-z]d",&no)) // 原句是 scanf("%d",&no);
puts("error");
else
if ((*no) % 2)
puts("这个整数是奇数。");
else
puts("这个整数是偶数。");
printf("要重复一次吗? 【yes...0 / no ... 9]】:");
scanf("%d",&cont);
} while (cont == 0);
return (0);
}
(*no) % 2
int *no=(int *)malloc(sizeof(int));