0

I have a string variable of all digits that I am parsing into an array called hugeInt. Doing this in a loop where I pick off one digit at a time out of the string and trying to put into the array. I have tried the code below which uses std::stoi directly and it gives me an error I think because it is a string. So I also tried converting the digit using const char *digit = strInt[j].c_str() but this gives me an error also. So how do I get what I thought was a one character string (which is a digit) from a real string to convert to an int. code below. My .h file

// HugeInteger.h
#ifndef HUGEINTEGER_H
#define HUGEINTEGER_H

#include <array>

class HugeInteger {
   private:
      static const int SIZE = 40;
      std::array<short int,SIZE> hugeInt;
   public:
      HugeInteger(long = 0);
      HugeInteger(std::string strInt);
      void displayHugeInt();
};
#endif

my implementation code

// HugeInteger.cpp
#include <iostream>
#include <stdexcept>
#include "HugeInteger.h"

HugeInteger::HugeInteger(long int num) {
   for (short &element : hugeInt) {
      element = 0;
   }

   for (int i = SIZE-1; i >= 0; i--) {
      hugeInt[i] = num % 10;
      num /= 10;
      if (num == 0) {
         break;
      }
   }
}

HugeInteger::HugeInteger(std::string strInt) {
   for (short &element : hugeInt) {
      element = 0;
   }

   if (strInt.length() > 40) {
      throw std::invalid_argument("String integer is over 40 digits - too large.");
   }

   for (int i = SIZE-1, j = strInt.length()-1; j >= 0; i--, j--) {
      if (isdigit(strInt[j])) {
         hugeInt[i] = std::stoi(strInt[j]);
      }
      else {
         throw std::invalid_argument("String integer has non digit characters.");
      }
   }
}

void HugeInteger::displayHugeInt() {
   bool displayStarted = false;
   for (short &element : hugeInt) {
      if (!displayStarted) {
         if (element == 0) {
            continue;
         }
         else {
            std::cout << element;
            displayStarted = true;
         }
      }
      else {
         std::cout << element;
      }
   }
   std::cout << std::endl;
}

The problem is in the second constructor (for a string) in the for loop where hugeInt[i] = std::stoi(strInt[j]); is. Any help or suggestions welcomed and appreciated.

William S
  • 167
  • 2
  • 10
  • Possible duplicate of [Convert char to int in C and C++](https://stackoverflow.com/questions/5029840/convert-char-to-int-in-c-and-c) – JaMiT Oct 24 '19 at 01:30

2 Answers2

1

strInt[j] is a char, not a std::string. We can't std::stoi a single character, but since we know it is is a digit (thanks to isdigit(strInt[j])), we can cheat like heck and

hugeInt[i] = strInt[j] - '0';

to get the value of the digit because C++ guarantees that all digits are encoded to ascend from '0' and are contiguous. This means that '0' - '0' will be 0. '1' - '0' will be 1. '2' - '0' will be 2, and so on.

user4581301
  • 33,082
  • 7
  • 33
  • 54
  • I saw that cheat in the book I am reading and they did not explain it just used it. I am still hoping to get some other answers to increase my knowledge. Thanks for the explanation. – William S Oct 23 '19 at 22:13
  • An alternative would be `hugeInt[i] = std::stoi(std::string(strInt[j], 1));` - make me a temporary `string` from the `char` at `strInt[j]`, then convert this `string` to an `int` with `stoi`, but that's a lot more work, and guess what `stoi` is probably doing inside to turn the characters in the string into `int`s... – user4581301 Oct 23 '19 at 23:51
0

Actually since strInt[j] is a ch, I had to use a different string constructor. This one compiles and runs correctly hugeInt[i] = std::stoi(std::string(1,strInt[j]));.

William S
  • 167
  • 2
  • 10