Search code examples
c++arraysiostream

How to user input the array elements in c++ in one line


I am new to c++ , Basically I belong to PHP . So I am trying to write a program just for practice, to sort an array . I have successfully created the program with static array value that is

// sort algorithm example
#include <iostream>     // std::cout
#include <algorithm>    // std::sort
#include <vector>       // std::vector


bool myfunction (int i,int j) { return (i<j); }

struct myclass { bool operator() (int i,int j) { return (i<j);} } myobject;

int main () {
   int myints[] = {55,82,12,450,69,80,93,33};
  std::vector<int> myvector (myints, myints+8);               

  // using default comparison (operator <):
  std::sort (myvector.begin(), myvector.begin()+4);           

  // using function as comp
  std::sort (myvector.begin()+4, myvector.end(), myfunction); 

  // using object as comp
  std::sort (myvector.begin(), myvector.end(), myobject);     

  // print out content:
  std::cout << "myvector contains:";
  for (std::vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it)
    std::cout << ' ' << *it;
    std::cout << '\n';

  return 0;
}

its output is ok . But I want that the elements should input from user with space separated or , separated . So i have tried this

int main () {
    char values;
    std::cout << "Enter , seperated values :";
    std::cin >> values;
  int myints[] = {values};


  /* other function same */
}

it is not throwing an error while compiling. But op is not as required . It is

Enter , seperated values :20,56,67,45

myvector contains: 0 0 0 0 50 3276800 4196784 4196784

------------------ (program exited with code: 0) Press return to continue


Solution

  • You can use this simple example:

    #include <iostream>
    #include <string>
    #include <sstream>
    #include <algorithm>
    
    using namespace std;
    
    int main()
    {
        stringstream ss;
        string str;
        getline(cin, str);
        replace( str.begin(), str.end(), ',', ' ');
        ss << str;
    
        int x = 0;
        while (ss >> x)
        {
            cout << x << endl;
        }
    }
    

    Live demo


    or, if you want to have it more generic and nicely enclosed within a function returning std::vector:

    #include <iostream>
    #include <string>
    #include <sstream>
    #include <algorithm>
    #include <vector>
    
    using namespace std;
    
    template <typename T>
    vector<T> getSeparatedValuesFromUser(char separator = ',')
    {
        stringstream ss;
        string str;
        getline(cin, str);
        replace(str.begin(), str.end(), separator, ' ');
        ss << str;
    
        T value{0};
        vector<T> values;
        while (ss >> value)
        {
            values.push_back(value);
        }
    
        return values;
    }
    
    int main()
    {
        cout << "Enter , seperated values: ";
        auto values = getSeparatedValuesFromUser<int>();
    
        //display values
        cout << "Read values: " << endl;
        for (auto v : values)
        {
            cout << v << endl;
        }
    }
    

    Live demo