问个简单的程序练习……
C primer 4th 练习的8.8
作个这样的程序
有加减乘除功能
如果输入的不是数字,要有纠错功能
打印出“该输入不是数字”,然后提示重新输入
在除法项目,如果被除数是0,也要提示错误
重新输入```
我简单的功能做出来了,但是纠错功能不知道怎么处理
做出来的程序碰到错误输入就停止运行了
程序做好输出像下面的
enter the operation of your choice:
a. add s.subtract
m. multiply d.divide
q. quit
a
enter first number: 22.4
enter second number: one
one is not an number.
Please enter another number :1
22.4 + 1 = 23.4
enter the operation of your choice:
a. add s.subtract
m. multiply d.divide
q. quit
d
enter first number: 22.4
enter second number: 0
enter a number other than 0:0.2
18.4 / 0.2 = 92
enter the operation of your choice:
a. add s.subtract
m. multiply d.divide
q. quit
q
Bye
[解决办法]
isdigit()是检查参数是否为0-9的数字字符,这里不太合适
比较土的办法
printf( "Enter first number: ");
char buff[10];
gets(buff);
float num1 = atof(buff);
while (num1 == 0 && buff[0] != '0 ')
{
printf( "%c ", buff[0]);
printf( "is not an number\n ");
printf( "Enter first number: ");
gets(buff);
num1 = atof(buff);
}