Modular Calculator Wrong output

I have to take a number through a bunch of math and at the end, take the modular value after everything.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
using namespace std;
int main(){
   int num1, num2, ans = 0;
char oper = ' ';
cin >> num1>> oper >> num2;
//The first time
if (oper == '*')
    ans =num1*num2;
    else if (oper == '+')
    ans = num1+num2;
    //other times
do {cin>> oper >> num2;
if (oper == '*')
    ans =num2*ans;
    else if (oper == '+')
    ans = num2+ans;
    else
    ans = (ans % num2);} while (oper!='%');
cout<<ans;
}


Intput: 5058
* 45
* 7635
+ 23
* 4066
* 552
+ 9730
* 100
* 647
* 79
* 4168
* 74
* 66
* 1626
+ 23
* 5311
* 5
* 7926
+ 4
+ 72
* 67
+ 131
* 6
+ 38
+ 878
* 9
+ 94
* 876
+ 1053
* 460
* 96
* 50
+ 6
* 805
* 4
+ 5861
+ 470
+ 712
+ 6
* 2720
* 61
+ 98
* 135
* 721
* 9015
* 6979
* 6414
+ 2187
* 223
+ 1
* 273
* 7023
% 6661

Output: 4125
Expected: 2560

Double doesn't work because it can't be mod'd, unsigned int doesn't do the trick either.
Last edited on
You can use BigInt , then store all of that garbage in a variable.

BigInt reallybignumber = 234908234234097 * 2342390727393939393939;

Here is a good link on BigInt: https://mattmccutchen.net/bigint/
solution.cc:2:31: fatal error: BigIntegerLibrary.h: No such file or directory #include <BigIntegerLibrary.h> ^compilation terminated.

BigInt not defined.
Did you include the BigInt library?

i.e.: #include "BigIntegerLibrary.h"

Last edited on
solution.cc:2:31: fatal error: BigIntegerLibrary.h: No such file or directory #include "BigIntegerLibrary.h" ^compilation terminated.
yes.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include "BigIntegerLibrary.h"
using namespace std;
int main(){
   bigint num1, num2, ans = 0;
char oper = ' ';
cin >> num1>> oper >> num2;
//The first time
if (oper == '*')
    ans =num1*num2;
    else if (oper == '+')
    ans = num1+num2;
    //other times
do {cin>> oper >> num2;
if (oper == '*')
    ans =num2*ans;
    else if (oper == '+')
    ans = num2+ans;
    else
    ans = (ans % num2);} while (oper!='%');
cout<<ans;
}
Last edited on
You are correct, my mistake, I did not realize that guy's Big Integer implementation was in C and not C++. I'll look for another solution for you. I know you can use boost libraries but I've never used them before.
defining BigInt doesn't work either:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std;
typedef unsigned long long bigint;
int main(){
    bigint num1, num2, ans = 0;
char oper = ' ';
cin >> num1>> oper >> num2;
//The first time
if (oper == '*')
    ans =num1*num2;
    else if (oper == '+')
    ans = num1+num2;
    //other times
do {cin>> oper >> num2;
if (oper == '*')
    ans =num2*ans;
    else if (oper == '+')
    ans = num2+ans;
    else
    ans = (ans % num2);} while (oper!='%');
    cin>> oper >> num2;
}
You've encountered overflow. Since you're only adding positive integers and multiplying by positive integers, you can check to see if 'ans' wrapped around since the previous operation. This won't work if you incorporate division or subtraction. I had it print out the answer from the last computation and the answer from the current computation, and this it what it gave me:


5058
*
45
*
7635
Ans = 1737802350 | Old Ans = 0
+ 23
Ans = 1737802373 | Old Ans = 1737802350
* 4066
Ans = 7065904448618 | Old Ans = 1737802373
* 522
Ans = 3688402122178596 | Old Ans = 7065904448618
+ 9730
Ans = 3688402122188326 | Old Ans = 3688402122178596
* 100
Ans = 368840212218832600 | Old Ans = 3688402122188326
* 647
Ans = 17278688421070072808 | Old Ans = 368840212218832600
* 79
Ans = 18404067883738483864 | Old Ans = 17278688421070072808
* 4168
Ans = 6593080937685125824 | Old Ans = 18404067883738483864
Overflow
Press any key to continue . . .


Using a BigInt class, I got this:

5058
* 45
* 7635
Ans = 1737802350 | Old Ans =
+ 23
Ans = 1737802373 | Old Ans = 1737802350
* 4066
Ans = 7065904448618 | Old Ans = 1737802373
* 552
Ans = 3900379255637136 | Old Ans = 7065904448618
+ 9730
Ans = 3900379255646866 | Old Ans = 3900379255637136
* 100
Ans = 390037925564686600 | Old Ans = 3900379255646866
* 647
Ans = 252354537840352230200 | Old Ans = 390037925564686600
* 79
Ans = 19936008489387826185800 | Old Ans = 252354537840352230200
* 4168
Ans = 83093283383768459542414400 | Old Ans = 19936008489387826185800
* 74
Ans = 6148902970398866006138665600 | Old Ans = 83093283383768459542414400
* 66
Ans = 405827596046325156405151929600 | Old Ans = 6148902970398866006138665600
* 1626
Ans = 659875671171324704314777037529600 | Old Ans = 405827596046325156405151929600
+ 23
Ans = 659875671171324704314777037529623 | Old Ans = 659875671171324704314777037529600
* 5311
Ans = 3504599689590905504615780846319827753 | Old Ans = 659875671171324704314777037529623
* 5
Ans = 17522998447954527523078904231599138765 | Old Ans = 3504599689590905504615780846319827753
* 7926
Ans = 138887285698487585147923394939654773851390 | Old Ans = 17522998447954527523078904231599138765
+ 4
Ans = 138887285698487585147923394939654773851394 | Old Ans = 138887285698487585147923394939654773851390
+ 72
Ans = 138887285698487585147923394939654773851466 | Old Ans = 138887285698487585147923394939654773851394
* 67
Ans = 9305448141798668204910867460956869848048222 | Old Ans = 138887285698487585147923394939654773851466
+ 131
Ans = 9305448141798668204910867460956869848048353 | Old Ans = 9305448141798668204910867460956869848048222
* 6
Ans = 55832688850792009229465204765741219088290118 | Old Ans = 9305448141798668204910867460956869848048353
+ 38
Ans = 55832688850792009229465204765741219088290156 | Old Ans = 55832688850792009229465204765741219088290118
+ 878
Ans = 55832688850792009229465204765741219088291034 | Old Ans = 55832688850792009229465204765741219088290156
* 9
Ans = 502494199657128083065186842891670971794619306 | Old Ans = 55832688850792009229465204765741219088291034
+ 94
Ans = 502494199657128083065186842891670971794619400 | Old Ans = 502494199657128083065186842891670971794619306
* 876
Ans = 440184918899644200765103674373103771292086594400 | Old Ans = 502494199657128083065186842891670971794619400
+ 1053
Ans = 440184918899644200765103674373103771292086595453 | Old Ans = 440184918899644200765103674373103771292086594400
* 460
Ans = 202485062693836332351947690211627734794359833908380 | Old Ans = 440184918899644200765103674373103771292086595453
* 96
Ans = 19438566018608287905786978260316262540258544055204480 | Old Ans = 202485062693836332351947690211627734794359833908380
* 50
Ans = 971928300930414395289348913015813127012927202760224000 | Old Ans = 19438566018608287905786978260316262540258544055204480
+ 6
Ans = 971928300930414395289348913015813127012927202760224006 | Old Ans = 971928300930414395289348913015813127012927202760224000
* 805
Ans = 782402282248983588207925874977729567245406398221980324830 | Old Ans = 971928300930414395289348913015813127012927202760224006
* 4
Ans = 3129609128995934352831703499910918268981625592887921299320 | Old Ans = 782402282248983588207925874977729567245406398221980324830
+ 5861
Ans = 3129609128995934352831703499910918268981625592887921305181 | Old Ans = 3129609128995934352831703499910918268981625592887921299320
+ 470
Ans = 3129609128995934352831703499910918268981625592887921305651 | Old Ans = 3129609128995934352831703499910918268981625592887921305181
+ 712
Ans = 3129609128995934352831703499910918268981625592887921306363 | Old Ans = 3129609128995934352831703499910918268981625592887921305651
+ 6
Ans = 3129609128995934352831703499910918268981625592887921306369 | Old Ans = 3129609128995934352831703499910918268981625592887921306363
* 2720
Ans = 8512536830868941439702233519757697691630021612655145953323680 | Old Ans = 3129609128995934352831703499910918268981625592887921306369
* 61
Ans = 519264746683005427821836244705219559189431318371963903152744480 | Old Ans = 8512536830868941439702233519757697691630021612655145953323680
+ 98
Ans = 519264746683005427821836244705219559189431318371963903152744578 | Old Ans = 519264746683005427821836244705219559189431318371963903152744480
* 135
Ans = 70100740802205732755947893035204640490573227980215126925620518030 | Old Ans = 519264746683005427821836244705219559189431318371963903152744578
* 721
Ans = 50542634118390333317038430878382545793703297373735106513372393499630 | Old Ans = 70100740802205732755947893035204640490573227980215126925620518030
* 9015
Ans = 455641846577288854853101454368618650330235225824221985218052127399164450 | Old Ans = 50542634118390333317038430878382545793703297373735106513372393499630
* 6979
Ans = 3179924447262898918019795050038589560654711641027245234836785797118768696550 | Old Ans = 455641846577288854853101454368618650330235225824221985218052127399164450
* 6414
Ans = 20396035404744233660178965450947513442039320465548750936243144102719782419671700 | Old Ans = 3179924447262898918019795050038589560654711641027245234836785797118768696550
+ 2187
Ans = 20396035404744233660178965450947513442039320465548750936243144102719782419673887 | Old Ans = 20396035404744233660178965450947513442039320465548750936243144102719782419671700
* 223
Ans = 4548315895257964106219909295561295497574768463817371458782221134906511479587276801 | Old Ans = 20396035404744233660178965450947513442039320465548750936243144102719782419673887
+ 1
Ans = 4548315895257964106219909295561295497574768463817371458782221134906511479587276802 | Old Ans = 4548315895257964106219909295561295497574768463817371458782221134906511479587276801
* 273
Ans = 1241690239405424200998035237688233670837911790622142408247546369829477633927326566946 | Old Ans = 4548315895257964106219909295561295497574768463817371458782221134906511479587276802
* 7023
Ans = 8720390551344294163609201474284465070294654505539306133122518155312421423071614479661758 | Old Ans = 1241690239405424200998035237688233670837911790622142408247546369829477633927326566946
% 6661
Ans = 2560 | Old Ans = 8720390551344294163609201474284465070294654505539306133122518155312421423071614479661758
Got Expected Result
Press any key to continue . . .
"you can check to see if 'ans' wrapped around since the previous operation".... What do you mean by that? How do I do that?
If you reach the maximum size you can store in a variable and then make that bigger, it will wrap around. If 1000 was the max for an unsigned integer, then adding 1 to it would make it wrap around and be equal to 0.

http://www.cplusplus.com/articles/DE18T05o/

In this case, you are only adding and multiplying positive numbers. Because of that, we are guaranteed that the number we get after the operation will not be smaller than the one that we had before the operation. IF we a get a smaller number than what we had before, then that signals that overflow occurred. This method will not work if you start subtracting or dividing.
so.... what can I do for the overflow. Let's suppose I can detect when I overflow. What do I do? How do I prevent it from reaching so high it will exceed int?
There isn't really. Using a BitInt class would help your problems.

Consider using Boost's gmp_int

http://www.boost.org/doc/libs/1_59_0/libs/multiprecision/doc/html/boost_multiprecision/tut/ints/gmp_int.html
Topic archived. No new replies allowed.