-1

I am supposed to get two big integer numbers (up to 600 digits) from console screen and write the result on the console again.

I defined two variables of type std::string to store two big integer numbers .I take their values from the user. To take the sum of that two numbers, I defined two vectors to store the digits of that two strings of numbers .

Here is the problem, when I try to loop through the vector to print the digits that I took from strings of numbers I get the following result .The Ascii values of the digits are printed on The Console. Could anyone tell me how to fix this problem please.

Note: The code is still not complete .

For the first string I took the numbers 9 8 7 6 5 4 3 2 1 from the user , on the console window I got the following result.

[0]57

[1]56

[2]55

[3]54

[4]53

[5]52

[6]51

[7]50

[8]49

    #include <iostream>
    #include <sstream>
    #include <vector>
    #include <algorithm>


    std::string Sum_Of_Two_Long_Integers()
    {
      std::string First_String ;
      std::string Second_String ;
      std::string Result_String ;

      std::cout << "Please enter the first number: " ;
      std::getline(std::cin, First_String);
      std::cout << "Please enter the second number: " ;
      std::getline(std::cin, Second_String);

      std::vector <int> First_String_Vector (First_String.length()) ;
      std::vector <int> Second_String_Vector (Second_String.length()) ;

      for(int Counter = 0 ; Counter  < First_String_Vector.size() ; ++ Counter)
      {
         First_String_Vector[Counter] = First_String[Counter] ;
         Second_String_Vector[Counter] = Second_String[Counter] ;
         std::cout << "[" << Counter << "]" << First_String_Vector[Counter] << std::endl ;
      }

      return Result_String ;
   }

   int main()
   {
      std::string Result_String  = Sum_Of_Two_Long_Integers() ;
      std::cout << "Result = " << Result_String << std::endl ;

      return 0 ;
   }
drescherjm
  • 9,653
  • 5
  • 43
  • 62
Mohamad
  • 37
  • 9
  • Use a bignum library. Like [GMP](https://gmplib.org) or another similar library - there are several (but `GMP` is pretty darn good, IMHO). – Jesper Juhl May 02 '19 at 16:26
  • 1
    @EJoshuaS I want some one to tell me how to fix my code .I do not want to take anyone else's code. – Mohamad May 02 '19 at 16:28
  • 1
    @moda there is *no way* that your code is *ever* going to match the performance of `GMP` or similar libraries that have had tens (if not hundreds) of man years invested in them by *experts*. Just use a library already, don't try to roll your own solution - if you make it work it's going to be slow and trying to make it fast could easily result in years of work. Just stand on the shoulder of giants and move on to more important tasks. – Jesper Juhl May 02 '19 at 16:33
  • @moda If this is a homework assignment that you have to write your own code you should mention that to avoid the long discussion about using some other big math library. – drescherjm May 02 '19 at 16:34
  • @moda -- You could have avoided the entire discussion of "big number libraries' by seeing that your issue has absolutely nothing to do with big number libraries. You are printing the integer version of ASCII characters, and the issue could have been duplicated with a 2 or 3 line `main` function, without any mention of big number libraries. – PaulMcKenzie May 02 '19 at 16:39
  • Do you really need the vectors to do the calculation? if you do (and think it will help) you may want to create a to vector function and to string function to convert back and forth, – drescherjm May 02 '19 at 16:40
  • @JesperJuhl You are right but I have to wright my own code because it is a homework . That was my bad . I had to mention that it was a homework – Mohamad May 02 '19 at 16:40
  • 1
    @moda Advice -- the title given to this thread does not describe your actual issue ("How to calculate the sum of two big integers"). You should do more debugging and inspection of your own code to determine the real issue, thus when doing so, create a better and more appropriate title to the thread. Persons now doing a search on SO will come across this page, and see that the issue has nothing to do with adding up big integers. – PaulMcKenzie May 02 '19 at 16:46
  • @PaulMcKenzie I am a new in this site so I do not know exactly what do . Can I now change the title ? – Mohamad May 02 '19 at 16:49
  • @moda Edit the title by pressing the "edit" link below the text window. – PaulMcKenzie May 02 '19 at 16:52
  • @PaulMcKenzie thanks a lot but I am a new English learner and I face a problem with express what exactly the title is supposed to be – Mohamad May 02 '19 at 16:58

1 Answers1

0
First_String_Vector[Counter] = First_String[Counter] ;
Second_String_Vector[Counter] = Second_String[Counter] ;

The digits are stored as ASCII in you string, you should convert to integer before placing them into the vector.

This would do the trick:

First_String_Vector[Counter] = First_String[Counter] - '0';
Second_String_Vector[Counter] = Second_String[Counter] - '0';

I would also add a check for valid input before populating your vectors to make sure that you only read digits:

if(First_String[Counter] < '0' || First_String[Counter] > '9' ||
   Second_String[Counter] < '0' || Second_String[Counter] > '9')
{
    std::cout << "Invalid input\n";
    return "":// Or better throw an exception
}

EDIT: '6' isn't equal to 6. The first one is a char, its value is ASCII for character '6', and the second is the integer 6.

ASCII is an encoding. Characters are mapped to some numbers. Value for '0' is 48, '1' is 49, ..., '9' is 57

To be even more precise C++ does not guarantee to use ASCII encoding (though I don't know of an implementation that does not use it), but it does guarantee that '0'...'9' have contiguous integer values. So '6' - '0' will give us integer 6.

Nellie Danielyan
  • 971
  • 7
  • 19
  • That solved my problem thanks a lot but could you please tell me why adding -'0' solve the problem and could you please tell me where I am supposed to add a check for valid input to make sure that First_String[Counter] >= '0' && First_String[Counter] <= '9' and the same for Second_String. – Mohamad May 02 '19 at 16:43
  • 2
    @moda -- **text** is encoded with numeric values. If you want to interpret a text character as a numeric value you have to convert the **encoded representation** into the corresponding value. That is, `'9'` does not necessarily have the value `9`. But the C and C++ languages guarantee that the encodings of the characters `'0' .. '9'` are contiguous and increasing. So `'0' - '0'` will give you the number `0`, and, since `'1'` is one more than `'0'`, `'1' - '0'` gives you the value `1`. Etc. – Pete Becker May 02 '19 at 16:51
  • @moda sorry for the late reply, updated the answer – Nellie Danielyan May 02 '19 at 20:02