Álv*_*aro 3 c++ numbers while-loop greatest-common-divisor
我被分配编写一个简化有理数的程序.我想要做的是计算gcd,然后用gcd除以数字.但该程序返回一个非常奇怪的错误.
代码:
void read_rational(int& num, int& den) {
char bar;
if (cin >> num >> bar >> den) {
cout << "hi";
int a = num;
int b = den;
while (b != 0) {
int r = a%b;
a = b;
b = r;
}
num /= b;
den /= b;
}
}
INPUT: 10/2 OUTPUT: Illegal instruction (core dumped)
INPUT: 90/8 OUTPUT: Illegal instruction (core dumped)
Run Code Online (Sandbox Code Playgroud)
我试过评论一些脚本.程序似乎只在while循环存在时崩溃.但我看不出它有什么问题.