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 9187113
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T19:41:35+00:00 2026-06-17T19:41:35+00:00

I am trying to read a number ( 12 digit ) as a string

  • 0

I am trying to read a number ( 12 digit ) as a string and then copy it into an integer array but not getting it right.

My code is here :

//header files
#include<iostream>
#include<string>

// namespace 
using namespace std ;

int main ()

{
    string input ;

    do
    {
        cout << "Make sure the number of digits are exactly 12 : ";
        cin >> input ;
    } while((input.length()) != 12 );

    int code[11] ; // array to store the code


    //change string to integer
    int intVal = atoi(input.c_str());


    //
    for (int i = 12; i >= 0 ; i--)
    {
        code[i] = intVal % 10;
        intVal /= 10 ;
        cout << code[i] ;
    }


    cout << endl ;
    //now display code
    for (int i = 0 ; i < 11 ; i++)
    {
        cout << code[i];
    }

    system ("pause") ;
    return 0 ;
}

So , for a basic input of 123456789101. it should be stored in the code[] array.

So when I am displaying the code loop I want to make sure it’s same as 123456789101.

but it’s coming to this :

during the code

for (int i = 0 ; i < 11 ; i++)
{
cout << code[i];
}

it shows 00021474836 , where I want it to display me the number back !

  • 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-17T19:41:36+00:00Added an answer on June 17, 2026 at 7:41 pm

    To turn string to int array, try:

    std::string input = "123456789101";
    int code[12];
    
    for (int i = 0 ; i < 12 ; i++)
    {
      code[i] = input.at(i) - '0';
    }
    

    Also intvVal is not big enough to hold 123456789101

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

Sidebar

Related Questions

I'm trying the following code to read a number of text files into array
First off, this is homework. I'm trying to read a 5 digit number into
Have a look at this code: I am trying to read in a number
I'm trying to read name, card number, expiry date etc on Credit Card. but
I am trying to read a number from an excelsheet using PHPExcel. The code
I'm trying to read an ifstream into a string, where I can set the
I'm here trying to convert 4-digit hexa into dec but didn't succeed. Here is
I`m trying to read my build version number from a text file, and then
I am trying to read the number of lines of a file I found
I am trying to read the number of line in a binary file using

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.