这该怎么改啊,分数计算器

void fenshu::yuefen() { //约分
int comdiv, a, b, temp;
a = abs(fenzi);
b = abs(fenmu);
{
if (a > b) {
temp = a % b;
while (temp != 0) {
int t;
t = temp;
a = b;
b = t;
temp = a % b;
}
comdiv = b;
}
if (a == b) {
comdiv = a;
}
if (a < b) {
temp = b % a;
}
while (temp != 0) {
int t;
t = temp;
b = a;
a = t;
temp = b % a;
}
}
fenzi /= comdiv;
fenmu /= comdiv;
}

img

你的变量fenzi、fenmu这些都是从哪里来的,类的成员变量吗?
报错是因为temp在a==b的时候没有被赋值,是未初始化的变量,在while (temp != 0)这里可能会出错。
代码修改如下:

void fenshu::yuefen() { //约分
    int comdiv, a, b, temp;
    a = abs(fenzi);
    b = abs(fenmu);
    {
        if (a > b) {
            temp = a % b;
            while (temp != 0) {
                int t;
                t = temp;
                a = b;
                b = t;
                temp = a % b;
            }
            comdiv = b;
        }
        else if (a == b) {
            comdiv = a;
        }
        else if (a < b) {
            temp = b % a;
            while (temp != 0) {
                int t;
                t = temp;
                b = a;
                a = t;
                temp = b % a;
            }
            comdiv = a;
        }
        
    }
    fenzi /= comdiv;
    fenmu /= comdiv;
}