How to convert a 16 digit C++03 string to a 16 number integer array? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5

How to convert a 16 digit C++03 string to a 16 number integer array?

I want to store a credit card number into an integer array for running some verifications... Now, I store it into a string so that it can input the number like this: 546330215173XXXX But now, I tried using the following way, but it fails: char*arr=new char[16]; int*ccn=new int[16]; string CCN; getline(cin,CCN); istringstream is(CCN); is.getline(arr,16); for(int i=0;i<16;i++) { ccn[i]=arr[i]-'0'; } Where am I wrong?

26th May 2017, 4:23 PM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
12 Answers
+ 8
Try this: #include <iostream> #include <sstream> #include <cstring> int main() { std::stringstream ss; std::string str; int* ccn = new int[16]; std::cin >> str; for (int i = 0; i < 16; i++) { ss.clear(); ss.str(""); ss << str[i]; ss >> ccn[i]; std::cout << ccn[i]; } return 0; }
27th May 2017, 3:29 AM
Hatsy Rei
Hatsy Rei - avatar
+ 10
From what I can see here, you are trying to copy a char array (string) into an int array by using the assignment operator. It doesn't work like this. A simple fix would be to convert the characters stored in the array into their integer values before assigning them to the int array. There are a few ways to do this. 0) atoi() - http://www.cplusplus.com/reference/cstdlib/atoi/ https://code.sololearn.com/czOgq2u6N5fc/?ref=app 1) Converting via ASCII. //This is a dirty fix for your code: string str; int* ccn=new int[16]; cin >> str; for(int i=0;i<16;i++) { ccn[i] = int(str[i]) - 48; }
26th May 2017, 5:36 PM
Hatsy Rei
Hatsy Rei - avatar
+ 7
Woots! Updated! Please refer again! Much simpler! :DDDDDDD
27th May 2017, 3:39 AM
Hatsy Rei
Hatsy Rei - avatar
+ 6
@Kinshuk Strings are, by default already character arrays, so I highly suggest doing string conversion to integer arrays. If you want to use stringstream, it is also fine. // Please wait while I get the code for this.
27th May 2017, 3:11 AM
Hatsy Rei
Hatsy Rei - avatar
+ 6
The problem I had earlier was with ss.clear() missing. I'm not sure if ccn[i] = 0 is still needed or not. Please give it a try by removing extra stuff. The temp_str is also kinda bloat to convert char to str. (Because stringstream only accepts string as parameter, and when I do << str[i] it doesn't work)
27th May 2017, 3:36 AM
Hatsy Rei
Hatsy Rei - avatar
+ 3
@Hatsy Rei Is it possible that we can just pop out only one digit from a string without spaces directly into an integer array's element?
27th May 2017, 3:16 AM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 3
@Hatsy Rei Thank You very much!
27th May 2017, 3:33 AM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 3
@Hatsy Rei We can even remove the ss.str(""); part when we are clearing the string everytime...
27th May 2017, 3:42 AM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 3
Thank You very much!
27th May 2017, 3:42 AM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 2
@Hatsy Rei In your code, you are converting string to an integer, not an array... Since My integer was a 16 digit one, I discarded the atoi method, or directly using string stream to take the number from the string (I assume its a better practice than atoi)... I was thus left with only one choice according to the knowlegde of C++ I have, use a char array. I now tried to use your explicit integer conversion from char and assign it again, but that failed for me as well... Can you please help me in how to tackle this ? Shall I split the larger string into a smaller string array and use atoi on every part?
27th May 2017, 3:06 AM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 2
@Hatsy Rei Thank you! Ill wait...
27th May 2017, 3:13 AM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 2
Sure! Ill try reducing things and inform you...
27th May 2017, 3:38 AM
Kinshuk Vasisht
Kinshuk Vasisht - avatar