How to read in user entered comma separated integers?

后端 未结 9 537
轮回少年
轮回少年 2020-12-07 02:14

I\'m writing a program that prompts the user for:

  1. Size of array
  2. Values to be put into the array

First part is fine, I create a dynamica

9条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-07 02:55

    #include
    using namespace std;
    int a[1000];
    int main(){
        string s;
        cin>>s;
        int i=0;
        istringstream d(s);
        string b;
        while(getline(d,b,',')){
            a[i]= stoi(b);
            i++;
        }
        for(int j=0;j

    This code works nicely for C++ 11 onwards, its simple and i have used stringstreams and the getline and stoi functions

提交回复
热议问题