我正在努力学习如何自己编写代码,并且在计算中遇到了一些困难。有人能解释一下为什么pf总是在下面返回0吗?
int main(void)
{
//solicit input from users
long long int num = get_long_long("Credit card no: ");
eprintf("%lld\n",num);
//initialize
int i =0;
int j =0;
int counter =0;
string status;
//find length of input
while (num>0)
{
num/= 10;
counter++;
}
printf("counter is %i\n",counter);
//Identify card type by prefix
int power=(counter-2);
eprintf("power is %i\n", power);
int dp = pow(10,power);
eprintf("divofp is %i\n", dp);
//prefix=num
long long int pf=(num/dp);
eprintf("pf is %lld\n",pf);
}发布于 2018-10-24 03:47:25
pf将始终为零,因为在while循环结束时,num设置为零。
因此,num/anything总是等于零。
调试的一种好方法是逐行遍历代码,并在每个时间点查看变量的值。
这可以帮助你缩小像这样的问题。
发布于 2018-10-24 04:39:53
问题是你从哪里得到号码的长度:
while (num>0)
num/=10;在此之后,num将始终为0,因此最终表达式将导致0,因为0/x =0 (x != 0)。
https://stackoverflow.com/questions/52960724
复制相似问题