While Loop Repeats

Hello, I'm trying to solve this problem:
http://coj.uci.cu/24h/problem.xhtml?pid=3031

The problem states that 2 integers are going to be the input.
These 2 integers should be divided and the output should be a mixed fraction of these 2 integers. With whitespace separating the output tokens.
Such as:
27 12
2 3 / 12
A line containing 0 0 will follow the last test case.
So an example could be:

Sample input
27 12
2460000 98400
3 4000
0 0
Sample output
2 3 / 12
25 0 / 98400
0 3 / 4000

My code has a problem that if I input a huge number, it will sometimes crash, and sometimes loop the cout of the answer of that large number nonstop, what can I do to fix this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std;

int main() {
    signed long iA, iB, i1, i2;
    cin >> iA >> iB;
    while (iA>0 && iB>0 && iA<= 2147483647 && iB <=2147483647) {
        if (iA==0 && iB==0 ) {
            break;}
        i1=iA/iB;
        i2=iA%iB;
        cout << i1 << " " << i2 << " / " << iB << endl;
        cin >> iA >> iB;
    }
    return 0;
}
Last edited on
iA>= 2147483647 && iB >=2147483647
Why?
Because the problem states that each test case contains two integers in the range [1; 2^31 – 1]. The first number is the numerator and the second is the denominator.
My bad it was supposed to be iA<= 2147483647 && iB <=2147483647
Last edited on
Input specification says what the user is allowed to input, not what the program should verify. So, your program should assume (and not verify) that the input numbers are from the specified range.

By the way, a variable of type int can not have a value greater than 2^31-1, anyway. If the user tries to input a bigger number, the variable will not have the exact value that was input.
iA>= 2147483647 && iB >=2147483647
That is unnecessary, because a signed long has a max size of 214...647.
https://msdn.microsoft.com/en-us/library/296az74e.aspx
Also, signed long, just replace it with int, because they're the same thing.
If the problem states that the input is in a certain range, then that's a contract. You don't need to check that the input is actually in that range, you just need to be able to handle all inputs in that range. If the input happens to not be in that range, your program is allowed to output garbage or even crash without breaking the contract.

if I input a huge number, it will sometimes crash, and sometimes loop the cout of the answer of that large number nonstop
I can't reproduce this or see any way it could happen.
Yeah, on many compilers, type long has the same range as int. The only common exception is 64-bit gcc compiler.
Ohh okay I got it now. Helios, it would happen if I had done an imput with a large number equal to or bigger than 12 digits, but now I've switched from cout and cin to printf and scanf and it appears to work.
Thank you all so much!
Can you provide a sample input that reproduces the problem?
For example if I would imput 987987987987 9 it would crash/loop for ever
but i switches cout to printf and it apparently solved it idk why
Here's my code fixed and uploaded to COJ.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <stdio.h>
using namespace std;

int main() {
    int iA, iB, i1, i2;
    scanf("%i %i", &iA, &iB);
    while (iA>0 && iB>0 && iA<= 2147483647 && iB <=2147483647) {
        if (iA==0 && iB==0 ) {
            break;}
        i1=iA/iB;
        i2=iA%iB;
        printf("%i %i / %i\n", i1, i2, iB);
        scanf("%i %i", &iA, &iB);
    }
    return 0;
}

Can't reproduce. It just exits immediately.
Topic archived. No new replies allowed.