c++ 关于以输入enter结束的问题
输入一串数字及符号,例如:1,2,3,4,5,10,100 要求:将上面的数值以数组形式表示出来。
即:a[0] = 1;
a[2] = 2;
.....
a[7] = 100;
希望求一个程式码,实现以上过程。
谢谢
程序代码:#include <iostream.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char *chars = new char[50];
cin >>chars;
int a[10];
//唉,偷个懒也被批
char *p = strtok(chars, ",");
for (int i = 0;;i++)
{
if (!p) break;
a[i] = atoi(p);
cout<< "a[" << i
<< "] = " << a[i] <<endl;
p = strtok(NULL, ",");
}
delete []chars;
return 0;
}
