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?
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;
}