Thread: multiple integer input

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    9

    multiple integer input

    I need to take in any arbitrary amount of integers on one line. Like "enter integers:" 2 3 4 1 5
    I do not know the amount that will be entered, so I am not sure how to go about doing this. I have to store the data in an array. Can someone give me a few hints on how to do this? thanks

  2. #2
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    Read in the entire line as a string. Then scan the string for your integers.
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

  3. #3
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    If you dont know the length of the line you will need to limit the size of the input with an array (or the getline function that Cshot suggested) or use a dynamic container, ie vector. Then use a loop until you encounter a '\n' character
    Couldn't think of anything interesting, cool or funny - sorry.

  4. #4
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Code:
    string str;
    vector<int> ivec;
    getline(cin,str);
    stringstream sin(str);
    do
    {
      int i;
      sin >> i;
      ivec.push_back(i);
    } while (sin);
    Not tested, but it goes somthing like that.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. integer input control (oh fun !!!)
    By MoAv in forum C Programming
    Replies: 1
    Last Post: 02-03-2009, 03:00 PM
  2. ensure input is only integer
    By rahulsk1947 in forum C++ Programming
    Replies: 3
    Last Post: 03-31-2007, 07:46 AM
  3. Replies: 5
    Last Post: 04-12-2006, 06:30 PM
  4. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 10:43 AM
  5. Linked Lists Integer addition ? HELP Please??
    By green_eel in forum C Programming
    Replies: 3
    Last Post: 03-12-2003, 04:36 PM