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.

Recommended Answers

All 4 Replies

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)
            {
                v.push_back(x);
            }
        }
    }

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

would it be possible not to use string functions

is it 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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.