big addition project

This code supposed to read up to 30 digits and get input from users twice. example input:
input first number: 12
input second number: 123458
result: 123470

I have no idea to do it. I found this code randomly and edit a lil bit and it does not work. A hand will be highly appreciated.

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
{

  char number1[size];
  int i;
  cout<<"Enter positive integers up to 30 digits in lenght"<<endl;

  for (i = 0; i<size; i++)
    {
      cin>>number1[i];
    }

  char number2[size];
  cout<<"Enter second positive integers up to 30 digits in lenght"<<endl;

  for (i = 0; i<size; i++)
    {
      cin>>number2[i];
    }

  int integer1[size];
  int integer2[size];

  cout<<"integer 1: ";
  for (i = 0; i<size; i++)
    {
      integer1[i] = number1[i] - 48; 
      cout<<integer1[i];
    }
  cout<<endl;

  cout<<"integer 2: ";
  for (i = 0; i<size; i++)
    {
      integer2[i] = number2[i] - 48;
      cout<<integer2[i];
    }
  cout<<endl;

  swap_values(integer1[size]);
  swap_values(integer2[size]);

  int sum[size];
  cout<<"sum: ";

  for (i = 0; i<size; i++)
    {
      sum[i] = integer1[i] + integer2[i];

      if (sum [i-1]>9)
        {
          sum[i] = sum[i] + 1;
        }

      if (sum[i]>9)
        {
          sum[i] = sum [i] - 10;
        }

      cout<<sum[i];
    }

  swap_values(sum[size]);

  return 0;
}
Hmm, so you want to take input for 2 30-digit long numbers, and be able to add them? Is that what you want? if yes, then you'll have to make two character array of 30 element blocks, store the 30digit numbers in it, type-cast into int form, and then add them using manual addition algorithm... (PS - Don't copy other programs, plagiarism is a bad habit...) :) If you want more help, post what exactly do you need help with...
Alright. Actually i thought the 30 characters arrays has been declared. No? I realized that i posted only half of the code. Yes, plagiarism is bad, but i couldn't help it :P. Do i need to do swap values in this code. Yes, you are right, i want to input twice for 30 digits long numbers.

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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <iostream>
using namespace std;

void swap_values(int variable);

const int size = 30;

int main(){

  char number1[size];
  int i;
  cout<<"Enter positive integers up to 30 digits in lenght"<<endl;

  while (!cin.i() == '\n' || cin.eof());


  for (i = 0; i<size; i++)
    {
      cin>>number1[i];
    }

  char number2[size];
  cout<<"Enter second positive integers up to 30 digits in lenght"<<endl;

  for (i = 0; i<size; i++)
    {
      cin>>number2[i];
    }

  int integer1[size];
  int integer2[size];

  cout<<"integer 1: ";
  for (i = 0; i<size; i++)
    {
      integer1[i] = number1[i] - 48; 
      cout<<integer1[i];
    }
  cout<<endl;

  cout<<"integer 2: ";
  for (i = 0; i<size; i++)
    {
      integer2[i] = number2[i] - 48;
      cout<<integer2[i];
    }
  cout<<endl;

  swap_values(integer1[size]);
  swap_values(integer2[size]);

  int sum[size];
  cout<<"sum: ";

  for (i = 0; i<size; i++)
    {
      sum[i] = integer1[i] + integer2[i];

      if (sum [i-1]>9)
        {
          sum[i] = sum[i] + 1;
        }

      if (sum[i]>9)
        {
          sum[i] = sum [i] - 10;
        }

      cout<<sum[i];
    }

  swap_values(sum[size]);

  return 0;
}

void swap_values(int variable [30])
{
  for (int i = 0; i<= size; i++)
    {
      swap(variable [i], variable [size-1-i]);
    }
}
help me. What is wrong with the code above?
The way you're taking input for the numbers. cin requires you to hit Enter, so it will take input for a number 60times in your program... Use a get function of some sort that accepts the character asa it is entered.....

Also, I doubt your swap function would work.. Coz when you swap all the values halfway through, you've reversed the array... And then when you go from half till the end, you end up reversing it back..

Line 55 and 59, your loop starts from 0, and you are checking an array member of -1... what do you think that will do? I guess it might be returning some garbage value... rectify that too...
Last edited on
Topic archived. No new replies allowed.