Converting string with commas to int c++ | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Converting string with commas to int c++

I have been messing around with converting strings to integers, but I would like to enter in multiple numbers on one line separated by comma's and run an operation on each number. how can I do this?

9th Dec 2016, 8:54 PM
Ben Hurst
Ben Hurst - avatar
1 Answer
0
The following code print each numbers 1 by 1, you can easilly modify it to store the values in a array. #include <stdio.h> #include <string.h> int main () { char str[] ="1,2,3,4,5"; char *pt; pt = strtok (str,","); while (pt != NULL) { int a = atoi(pt); printf("%d\n", a); pt = strtok (NULL, ","); } return 0; }
10th Dec 2016, 10:10 AM
David Pierret
David Pierret - avatar