Tony S
Tony S

Reputation: 45

Is there a size limit for std::cin buffer?

I want to get 300 numbers from the user and print out the largest number. First, I use this code to ask for input:

cout << "Enter the array\n";

Then I paste 300 number into the terminal and hit enter. Then use this code to capture the input:

int count = 300;
int inputArray[count];
for (int i = 0; i<count; ++i) {
    cin >> inputArray[i];
}

And the code doesn't work, it just asks for more and more numbers.

But when I paste in 150 numbers at a time, (for example, paste 1 to 150, hit enter, then paste 151 to 300, hit enter again) the code works just fine. So, I'm guessing there is a limit for the buffer on how many input it can handle. Am I right? How does std::cin works?

Side question: should I get the data from a file instead of using cin?

My full code:

#include <string>
#include <iostream>
using namespace std;
int main()
{
    int count = 300;
    cout << "Enter the numbers\n";
    int inputArray[count];
    for (int i = 0; i<count; ++i) {
        cin >> inputArray[i];
    }
    cout << "Got input\n";
    int bigNum = inputArray[0];
    for (int i=1; i<count; ++i) {
        int in = inputArray[i];
        if (in > bigNum) {
            bigNum = in;
        }
    }
    cout << "The largest number is " << bigNum << endl;
    return 0;
}

Upvotes: 2

Views: 5291

Answers (1)

IanM_Matrix1
IanM_Matrix1

Reputation: 1594

There is no specified size limit on IO buffers that I could find in the standard, but there may be for your OS input buffer.

Upvotes: 1

Related Questions