How to fix this simple script?!

Hello again :p

I coded this in order to sum two maximum 50 digits number (eg: 100000000000000000... + 10000000000000000... )

So I made an array like this " A[50]" and B[50]" then a "for" loop and then it sums A[1] with A[2]....

But the first "for" loop is wrong (the first loop sets all a[i] and b[i]s zero) but I really don't know why it's wrong?! please help me! here's the code:
(Assuming that all of the digits are maximum 4)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include<conio.h>
#include<iostream.h>
void main()
{
 clrscr();
 int a[50];
 int b[50];
 int i=0;
 int j=0;
 int K=0;
for (int v=0; v<=50; v=v+1) \\has infinite loop error!!!!!
{
 a[v]=0;
 b[v]=0;
}
for (;;)
  {
   cout<<"Please insert a number devided in 10th two times, -123 to finish";
   cin>>K;
   if (K==-123) break;
   cin>>a[i];
   i+=1;
  }
cout<<"Please type + to sum otherwise - before your 2nd number \n";
while (2==2)
  {
   cout<<"Please insert a number devided in 10th two times, -123 to finish";
   cin>>K;
   if (K==-123) break;
   cin>>b[j];
   j+=1;
  }
i=0; j=0; K=0;
while (i<=49)
  {
   K=a[i]+b[j]+K;
   i=i+1;
   j=j+1;
  }
 cout<<"  \n"<<K;
 getch();
}
Last edited on
for (int v=0; v<=50; v=v+1)

Array indices go from 0 to (size - 1). So this should be a < sign rather than a <= sign.
Thanks for your tricky answer! but a new problem happened:
When I want to sum 14+21 it says 8! because it sums 1+4+2+1!!! I can't solve it, any ideas how to solve that?
You could make this significantly more straight-forward if you used strings. I would agree if someone said that using strings to manipulate numbers is counter-intuitive and kinda hacky, but it does lend itself useful to extract individual digits.
Are you familiar with strings?
Yeah, I heard it's easier if strings were used,The problem is I don't know much about them :/ .
EDIT: Will learn it soon.
Last edited on
Just fixed it (without strings!), that was easy..... well medium for me :)

Rules to work with this script are explained in the example below:

5500 +5500:

Type in this way:

input: 0 enter 0 enter 5 enter 5 enter -123 enter j enter* 0 enter 0 enter 5 enter 5 enter -123 enter

output: 11000

code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include<conio.h>
#include<iostream.h>
#include<math.h>
void main()
{
 clrscr();
 int a[50];
 int b[50];
 int i=0;
 int j=0;
 int MTV=0;
for (int v=0; v<50; v=v+1)
{
 a[v]=0;
 b[v]=0;
}
for (;;)
  {
   cout<<"Please insert a number devided in 10th two times, -123 to finish";
   cin>>MTV;
   if (MTV==-123) break;
   cin>>a[i];
   i+=1;
  }
cout<<"Please your 2nd number \n";
char key;
cout<<"Please say j if it's jam or k if it's kam :D";
key=getch();
if (key=='j') {
while (2==2)
  {
   cout<<"Please insert a number devided in 10th two times, -123 to finish";
   cin>>MTV;
   if (MTV==-123) break;
   cin>>b[j];
   j+=1;
  }
}
else if (key=='k')
{
while (2==2)
  {
   cout<<"Please insert a number devided in 10th two times, -123 to finish";
   cin>>MTV;
   if (MTV==-123) break;
   cin>>b[j];
   b[j]=b[j]*(-1);
   j+=1;
  }
}
i=0; j=0; MTV=0; int c[50]; int vedadi=0;
while (i<=49)
  {
   c[i]=a[i]+b[i]+vedadi;
   vedadi=0;
   if (c[i]>10)
   {
    vedadi=c[i]/10;
    c[i]=c[i]%10;
   }
   MTV=(c[i]*pow(10,i))+MTV;
   i=i+1;
   j=j+1;
  }
 cout<<"  \n"<<MTV;
 getch();
}


*"Jam" is + And "Kam" is - .
Last edited on
Topic archived. No new replies allowed.