0

I have these variables.

static constexpr int size{70};
const char* str;
std::array<int_least8_t, size> digits;

if str is "1110" I want it to be in digits [1,1,1,0]

int i=0;
while (*cci)
{
    digits[i]=*cci; // I need some method to convert it
    +cci;
    i++;
}
anastaciu
  • 23,467
  • 7
  • 28
  • 53
error
  • 3
  • 3
  • 4
    `digits[i] = *cci - '0';` – Eljay Aug 12 '20 at 11:16
  • 2
    Does this answer your question? [Convert char to int in C and C++](https://stackoverflow.com/questions/5029840/convert-char-to-int-in-c-and-c) – anastaciu Aug 12 '20 at 11:18
  • Better duplicate: [Convert a character digit to the corresponding integer in C](https://stackoverflow.com/q/628761/6865932). – anastaciu Aug 12 '20 at 15:48

1 Answers1

2

I made example for you here

#include <iostream>
#include <array>
#include <string>
#include <algorithm>

static constexpr int size{70};
const char* str = "1110";
std::array<int_least8_t, size> digits;

int main()
{
    int i = 0;
    for(auto c = str; *c; ++c, ++i) {
        digits[i] = *c - '0';
    }
        
    std::cout << "Count=" << i << std::endl;
    std::for_each(std::begin(digits), std::begin(digits) + i, [](const auto& Value) {
        std::cout << "Value=" << std::to_string(Value) << std::endl;
    });
    
    return 0;
}

Output:
Count=4
Value=1
Value=1
Value=1
Value=0

In MSVC compiler I use it looks like

typedef signed char int_least8_t;
Amir Kadyrov
  • 1,253
  • 4
  • 9
  • may i ask why you subtract with `'0'`? Also why are you using the `std::for_each` instead of a range-based for-loop? I believe `for_each` shouldn't be used in this case... – Amachi Aug 12 '20 at 11:29
  • Allthough the code is really clean and uses modern C++ features (great!) it lacks explanation. Explaining _why_ something works is far more important than just providing a working solution. – Lukas-T Aug 12 '20 at 11:34
  • Amachi, cuz I need print only 4 elements, not 70. – Amir Kadyrov Aug 12 '20 at 11:34
  • churill, Sorry, I'm interested in write code, but not give an explanations cuz I'm lazy. Tell me if I should delete my answer. – Amir Kadyrov Aug 12 '20 at 11:38
  • @churill can you tell me why isn't this working on my code when I write digits[i]=*cci - '0' – error Aug 12 '20 at 11:40
  • @akk0rd87 You shouldn't delete your answer, but [edit] it. You should be able to explain why you wrote the code this way and why you think this is the best way to do it. – Lukas-T Aug 12 '20 at 11:48
  • @churill when I change int_least8_t to just int it works fine. But I need it to stay int_least8_t. does anybody know why is it like this? – error Aug 12 '20 at 11:49
  • @akk0rd87 yes I see that your is working, but why I cant print it the easy way cout< – error Aug 12 '20 at 11:58
  • @akk0rd87 how does the digits array look like? are there 1 1 1 0 integers or what? – error Aug 12 '20 at 12:02
  • @error, because In MSVC compiler I use it looks like typedef signed char int_least8_t; – Amir Kadyrov Aug 12 '20 at 12:02
  • @akk0rd87 you should use there you will find int_least8_t – error Aug 12 '20 at 12:07