How do I split a cin << string into integers?

Posted on 2009-04-21
Last Modified: 2012-05-06
Hey All,

I'm looking for a way to input 10 ints from a string.

I've tried the code below it doesn't work, since it is expecting th NULL. And other examples on here use char with built in text so that you can use NULL in the while loop. I then tried with the EOL but I run into char to int comparisons.

Ideally, I'd liek something simple like Java

public class Main {

     * @param args the command line arguments
    public static void main(String[] args) {
        for (String s_in: args){

Thank you


	char inputString[200];

	const char EOL = '\n';

	char delims[] = " ";

	char *result = NULL;

	result = strtok(inputString, delims);

	while (result != EOL)


		// conver to int and add to Q here















Open in new window

Question by:cflores89
    LVL 39

    Accepted Solution

    See if the example below helps. It assumes input is your string that you've already obtained from some other source. We put it into a stringstream and use this to pull out the int types. The example doesn't contain error handling, I leave that up to you.
    #include <iostream>
    #include <sstream>
    #include <deque>
    int main()
    	char const input[] = "0 1 2 3 4 5 6 7 8 9\n";
    	std::stringstream ss;
    	ss << input;
    	std::deque<int> myQueue;
    		int i;
    		ss >> i;
    		if(ss.eof()) { break; }
    		if(ss.bad()) { /* handle error */ break; }
    	for(std::deque<int>::const_iterator itr = myQueue.begin() ; itr != myQueue.end() ; ++itr)
    		std::cout << *itr << std::endl;

    Open in new window

    LVL 39

    Assisted Solution

    You can do

       string input;
       getline(cin, input);  // reads until the user types <enter>
       istringstream iss(input);
       int arr[100] = { 0 };
       int n = 0;
       while (n < (int)sizeof(arr)/sizeof(arr[0]) && iss>>arr[n])

    Then n is the number of integers entered.

    >>>> Ideally, I'd liek something simple like Java
    The Java code takes strings from an array and not integers from a string.

    Author Closing Comment

    Thank you very much! :)

    stringstream was what i was looking for

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
    Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
    The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
    The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now