input with cin....

This is a discussion on input with cin.... within the C++ Programming forums, part of the General Programming Boards category; Hello everybody, I've just got an assignment, I need to write a program that receives as input an unknown amount ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    1

    Question input with cin....

    Hello everybody,
    I've just got an assignment, I need to write a program that receives as input an unknown amount of numbers in one line with space after each number, and enter after the last number. I need to get those numbers with the 'cin' command.
    How can I do that?
    Since I don't know how many numbers, I thought about placing cin in a loop but how can it recognize when the line has ended?

    Thank you for you help!!

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    you could try char by char read in to a char array and convert each char array to a string and then to an int or a float when you've found a space using atoi or atof. Alternatively, you could try calling the peek function to see what the terminating char for each >> call is before calling the next >> since >> doesn't clear the terminating char from the input buffer.

  3. #3
    Registered User
    Join Date
    Feb 2004
    Posts
    127

    Re: input with cin....

    cin>> skips spaces and newlines so i think cin.get() will be better which read char by char

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    220
    Code:
    #include <iostream>
    #include <vector>
    
    using namespace std;
    
    
    int main()
    {
      vector<int> numbers;
      int num;
    
      cout << "Enter a list of numbers and then q and enter to exit: ";
      while ( cin>>num )
        numbers.push_back ( num );
      //do something with numbers..
      return 0;
    Note: This came from Preludes code example at:
    Problems with array to string assignment.


    This semi solves your problem, but i'm not entirely sure how you would deal with the enter portion of the solution..
    OS: Windows XP Pro CE
    IDE: VS .NET 2002
    Preferred Language: C++.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem grabbing k/b input
    By falcon9 in forum C Programming
    Replies: 2
    Last Post: 10-28-2007, 12:47 PM
  2. cin input error
    By justlivelife15 in forum C++ Programming
    Replies: 4
    Last Post: 06-18-2007, 12:29 PM
  3. Trouble with a lab
    By michael- in forum C Programming
    Replies: 18
    Last Post: 12-06-2005, 11:28 PM
  4. need help with some input
    By blindleaf in forum C Programming
    Replies: 2
    Last Post: 03-16-2003, 01:50 PM
  5. c++ string input
    By R.Stiltskin in forum C++ Programming
    Replies: 4
    Last Post: 02-22-2003, 04:25 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21