ive declared and integer array and i need to know how the user can input a single input of "2 4 6 3 5 6 7 5 3 5 6" and get each individal digit into each index of the array and help would be greatly appreciated.

6 Years
Discussion Span
Last Post by deceptikon

I'm assuming you're reading the entire input into a single string. You'll need to first break this string up into individual "words", or items separated by whitespace. Then each word will need to be converted to an integer and assigned to the array.

For example:

#include <iostream>
#include <iterator>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

int main()
    vector<int> v;
    string input;

    cout << "Enter a sequence of numbers: ";

    if (getline(cin, input))
        istringstream iss(input);
        string num;

        while (iss >> num)
            istringstream converter(num);
            int x;

            if (converter >> x)

    copy(v.begin(), v.end(), ostream_iterator<int>(cout, " "));
    cout << endl;

would it be possible not to use string functions


For any question that starts with "is it possible", the answer is nearly always going to be yes. Yes, it's possible. No, it's not as easy, safe, or robust without more effort and great care on your part.

Since this is starting to seem like homework and you still haven't proven that you've attempted anything, I'll refrain from offering a manual parsing example. Instead, I'll ask for details on your assignment, because you clearly have unreasonable restrictions that cannot be assumed.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.