Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9019869
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T04:52:32+00:00 2026-06-16T04:52:32+00:00

I am a beginner in C++ and cannot figure this out. Simply as the

  • 0

I am a beginner in C++ and cannot figure this out. Simply as the question says, if I have a string (of a number), how can I convert each digit to an integer and put each into an array of integers?

Here was my attempt:

std::string stringNumber = "123456789"; // this number will be very large

int intNumber[stringNumber.length()];

for (int i = 0; i < stringNumber.length(); i++) 
{
    intNumber[i] = std::atoi(stringNumber[i]);
    std::cout << intNumber[i] << std::endl;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T04:52:34+00:00Added an answer on June 16, 2026 at 4:52 am

    For a mostly prebuilt solution, I would use a vector and std::transform. The idea is to go through each character and add the integer form of it to the vector: (see full sample)

    std::string s = "1357924680";
    std::vector<int> ints;
    ints.reserve(s.size()); //to save on memory reallocations, thanks Nawaz
    std::transform(std::begin(s), std::end(s), std::back_inserter(ints),
        [](char c) {
            return c - '0';
        }
    );
    

    What this does is loop through from the beginning of the string to the end, take each character, and add the value of it minus '0' ('5' - '0' would be 5) to the end of the vector, leaving you with a vector of the integer equivalents. Best of all, it doesn’t reinvent the wheel!

    It also solves two problems you have:

    1. It uses std::vector instead of a variable-length array (VLA). The latter is nonstandard, so prefer a vector if you need a runtime-sized array.

    2. You use atoi, which is bad in itself because it returns 0 on errors, leaving no certainty of whether the result was 0 or whether there was an error. It also takes a string, not a single character. Instead this finds the distance between '0' and the character, which is the integer we need.

    For an additional layer of security, you can use isdigit to check whether the character is a digit. If not, raise an error of some sort (like an exception), or deal with it however else you would. This ensures the digits in your ints vector will be from 0 to 9 and you won’t end up with a digit of, say, 25.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sorry for asking such a beginner question but I can't figure this out. I
Beginner level question Scenario: Have simple string cocantation tool, that I might expand later
This is a beginner's question but for some reason I cannot find the answer
This is a very basic question, I know, but I cannot seem to figure
Beginner question. How come I can do this: Public Class Form1 Private StudentsInMyRoom As
probably this is a real beginner question, but i cannot find an answer on
Beginner question: I have a dictionary where the values are lists of (a variable
Total beginner question about jQuery: I have a Form that contains several TextBoxes (input
hello i'm a beginner java developer and this is one question in a list
I'm a beginner to Python, but I've been trying this syntax and I cannot

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.