-3

As Mehdi Algholipour wrote i wanted to

1.Get string from input

2.Separate input to Integer numbers

3.Save them into Array

    cout << "Give me some integers separated with space";

    cin >> string;     // example input 10 210 300 510


    //..some code that seperate input to integer numbers and save them to Array


    //EXPECTED RESULT: Array[0]=10 Array[1]=210 etc...
Jacob
  • 345
  • 1
  • 8
  • 17
  • 1
    `int array_of_ints[4]; for (int i = 0; i < sizeof(array_of_ints); ++i) cin >> array_of_ints[i];` is that what you want? –  Aug 02 '14 at 11:24
  • Otherwise I don't get what you mean by array of integers then you say you want every string until space and put into array of strings, your question is not clear... –  Aug 02 '14 at 11:24
  • You could for instance use a solution like it is [proposed here](http://stackoverflow.com/a/24520662/1413395). – πάντα ῥεῖ Aug 02 '14 at 11:25
  • Sorry that I didn't make it easier. – Jacob Aug 02 '14 at 11:26
  • Do you mean you have a string that contains numbers and want to extract those numbers into an array of integers? –  Aug 02 '14 at 11:27
  • user9000 - yes !!!! :D – Jacob Aug 02 '14 at 11:29

3 Answers3

1

Try:

#include <iostream>
#include <string>
#include <vector>
#include <regex>

int main()
{
    std::regex rgx("\\b\\d+\\b");

    std::string line;

    std::cout << "Please enter numbers separated by spaces and press enter:" << std::endl;

    std::getline(std::cin, line);

    std::sregex_iterator it(line.begin(), line.end(), rgx);
    std::sregex_iterator end;

    std::vector<int> values;
    std::transform(it, end, std::back_inserter(values), [](std::smatch s){ return std::stoi(s.str()); });

    for (int v : values)
        std::cout << v << std::endl;

}
Brandon Kohn
  • 1,612
  • 8
  • 18
1

I think your mean is:

  1. Get string from input
  2. Separate input to Integer numbers
  3. Save them into Array

If this is your mean, try this code:

string str;
int arr[1000];  // I supposed 1000 is your Int Array size.
int number = 0, index = 0;

getline(cin, str);  // Get a line of string

for (int i = 0 ; i < str.length() ; i++){   // Processing your str Array
    if (isdigit(str[i])){
        number *= 10;
        number += ((int)str[i] - '0');
    }
    else {
        arr[index++] = number;
        number = 0;
    }
}
arr[index] = number;    // for last element of your input

// Do something you want
  • 1
    @Jacob isdigit(int c) function need an argument that it is `ASCII Code` of an character. When given `ASCII Code` is an digit (0-9) it returns `true` otherwise `false`. – Mehdi Algholipour Aug 02 '14 at 15:31
  • about your second question, lets suppose our input is `12`. This code firs reads `1` and sets `number *= 10; => number = 0 * 10 = 0; number += ((int)str[0] - '0'); => number = number + 1 = 1`. Next step, reads `2`. Now sets `number *= 10; => number = 1 * 10 = 10; number += ((int)str[1] - '0'); => number = number + 2 = 12`. – Mehdi Algholipour Aug 02 '14 at 15:54
0

Use stringstream:

string str;
getline(cin, str);

stringstream ss(str);
vector<int> vec;

int Integer;
while(true)
{
    ss >> Integer;
    if(!ss)
        break;

    vec.push_back(Integer);
}

live demo

Note: You need to include following headers: <string>, <sstream>, <vector>

Quest
  • 2,764
  • 1
  • 22
  • 44