为什么这个C++代码不能用于负整数?

-5 c++ integer

#include<iostream>
using namespace std;
int x;
int y;
int z;
int main ()
{
    cout << "x=";
    cin >> x;
    z = x;
    y = 0;
    while(z > 0)
    {
        (z = z / 10);
        (y = y + 1);
    }
    while(z < 0)
    {
        (z =- z);
        (z = z / 10);
        (y = y + 1);
    }
    cout << "cifre=" << y;
}
Run Code Online (Sandbox Code Playgroud)

它似乎对正整数有效,但不适用于负整数,我不明白为什么.谁能解释一下?它必须计算位数......

Joh*_*hnB 7

它可能不会对负整数"起作用",因为zwhile (z<0)循环的每次迭代中都会翻转符号.因此,循环将在第一次迭代后停止,y为1.