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 !
To turn string to int array, try:
Also intvVal is not big enough to hold
123456789101