Input buffer question

This is a discussion on Input buffer question within the C++ Programming forums, part of the General Programming Boards category; Hi, I'm new to the boards and C++. I was wondering how I could read in multiple values from a ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    2

    Input buffer question

    Hi,

    I'm new to the boards and C++. I was wondering how I could read in multiple values from a line of data. I have a part of the program I was working on, here:

    Code:
    	cout << "Please enter" << x << " samples." << endl;
    	cin >> sample;
    
    	for (i = 1; i <= x; i++)
    	{
    		cout << "Sample " << i << " is " << sample << endl;
    	}

    basically, what i am having problems with is reading in the "x" samples from the input buffer. I can do it with characters, but not numbers. Can anyone help?

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    Code:
    cout << "Please enter" << x << " samples." << endl;
    for (int i = 0; i < x; i++)
       cin >> sample[x];
    Something like that?
    Sent from my iPadŽ

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    It depends on how the data is entered by the user. The >> operator is programmed to skip over any leading whitespace(spaces, tabs, newlines) and read in data until it encounters a whitespace character at which point it stops. So, if the user enters 3 numbers like this:

    123

    >> will read in 123--not 1. However, if the user enters data like this:

    1 2 3

    then >> will read in 1, and then the next time >> is used, it will read in 2, etc. In the latter case, if you want to read all the numbers at once, you can do this:

    cin>>sample1>>sample2>>sample3;
    Last edited by 7stud; 02-01-2006 at 08:04 PM.

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    2
    yeah... guess so...but i want to do it for an unknown number of entries that the user would have entered in as 'x'

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Code:
    	#include <vector>
    	using namespace std;
    .
    .
    	vector<int> sample;
    
    	cout << "Please enter" << x << " samples." << endl;
    	int tmp;
    	for (int i=0; i<x; ++i)
    	{
    		cin >> tmp;
    		sample.push_back(tmp);
    	}
    
    	for (int i=0; i<sample.size(); ++i)
    	{
    		cout << "Sample " << i << " is " << sample[i] << endl;
    	}
    Or what SlyMaelstrom posted which uses an array.
    Last edited by swoopy; 02-01-2006 at 09:21 PM. Reason: Code corrected

  6. #6
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    Vector of course being more modern and potentially more useful depending on where you wanted to go from there.
    Sent from my iPadŽ

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. Flush Input Buffer
    By zx-1 in forum C Programming
    Replies: 41
    Last Post: 10-29-2005, 08:58 AM
  3. File input question
    By Beast() in forum C Programming
    Replies: 16
    Last Post: 07-09-2004, 03:23 PM
  4. Input Buffer
    By Padawan in forum C++ Programming
    Replies: 4
    Last Post: 04-05-2004, 06:12 PM

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