跳过的条件,我不知道为什么

时间:2016-09-19 14:45:13

标签: c++ debugging

我正在调试这个,调试器跳过最后一个'if'偶数'sum'等于'n'并直接跳到'else',我不知道为什么。请帮忙。

P / s:我可以使用动态数组来提高程序的移动性吗?

#include <iostream>
#include <math.h>
using namespace std;

int exponent_of_10();                // set position for digits
int exponent_of_10(int a, int b){
for(int j = b; j>0;j--)
{
    a *= 10;
}
return a;
}

main() //check if the number was palindromic 
{
int n;  
int a[6]={0,0,0,0,0,0}; 
int i = 0;

int temp;
int S;

cout<< "Input n (maximum of 6 digits): ";
cin>> n;
do
{
    if(n<1)
    {break;}

    temp=n%10;
    a[i]=temp;
    n=(n-temp)/10;
    i++;
}
while (n!=0);

int sum = 0;
for(int j=0; j<=5; j++)
{
    exponent_of_10(a[j],j);
    S = exponent_of_10(a[j],j);
    if (S==0)
    {break;}
    sum +=S;
}

if(sum==n)
{
    cout<< "Congratz, this is PALIDROMIC NUMBER !!";
}
else
cout<< "Sad life, this is NOT palidromic number";

return 0;
}

1 个答案:

答案 0 :(得分:1)

当代码退出do ... while()循环时,n为0.要使if中的测试有意义,代码应保存n的原始值某处并将sum与原始值进行比较。