如果小数部分的前 3 位包含“9”,则应显示此代码,但不起作用。令人惊讶的是,“mod”变量对于任何数字都是 0。
int main( void )
{
float number, dmod;
int mod;
double digit_1, digit_2, digit_3;
double search=9;
cout<<"Enter the number:";
cin>>number;
mod = modf(number, &dmod);
digit_1 = mod /100 % 10;
digit_2 = mod /10 % 10;
digit_3 = mod /1 % 10;
if( (digit_1 == search) || (digit_2 == search) || (digit_3 ==search) )
{
cout<<"mod contains 9"<<endl;
}
else
{
cout<<"mod does not contains 9"<<endl;
}
}