Search code examples
c++stringvector

Console output displays ASCII values instead of expected digits of a number


I'm facing an issue with printing the individual digits of two big integer numbers in C++. The numbers are up to 600 digits in length, and I'm trying to store and manipulate them using strings and vectors. However, when I attempt to loop through the vector and print the digits, I get unexpected results – the ASCII values of the digits are being displayed on the console. I'm seeking guidance on how to resolve this issue.

Here's an example of the issue I encountered:

For the first string, I input the numbers 9 8 7 6 5 4 3 2 1, but on the console, I get the following result:

[0]57
[1]56
[2]55
[3]54
[4]53
[5]52
[6]51
[7]50
[8]49
#include <iostream>
#include <vector>

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] - '0'; // Convert char to integer
    Second_String_Vector[Counter] = Second_String[Counter] - '0'; // Convert char to integer
    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;
}

I believe the issue might be related to how I'm handling the ASCII values of the characters. Can someone help me understand how to correctly extract and print the individual digits without encountering this ASCII representation?


Solution

  • 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.